macro_rules! magic_number {
    ($name:ident, $value:expr) => {
        const $name: u32 = $value;
    };
}
magic_number!(KEYC_VI, 0x20000000);
magic_number!(KEYC_SENT, 0x40000000);
magic_number!(CLIENT_ACTIVEPANE, 0x80000000);

extern "C" {
    pub type hyperlinks;
    pub type screen_write_cline;
    pub type screen_sel;
    pub type screen_titles;
    pub type event_base;
    pub type evbuffer;
    pub type bufferevent_ops;
    pub type args;
    pub type tmuxpeer;
    pub type environ;
    pub type options;
    pub type format_tree;
    pub type input_ctx;
    pub type cmds;
    pub type tty_key;
    pub type tty_code;
    pub type format_job_tree;
    pub type control_state;
    pub type cmdq_list;
    pub type cmdq_item;
    fn ioctl(__fd: i32, __request: u64, _: ...) -> i32;
    fn __errno_location() -> *mut i32;
    fn fcntl(__fd: i32, __cmd: i32, _: ...) -> i32;
    fn open(__file: *const i8, __oflag: i32, _: ...) -> i32;
    fn write(__fd: i32, __buf: *const (), __n: size_t) -> ssize_t;
    fn usleep(__useconds: __useconds_t) -> i32;
    fn getpid() -> __pid_t;
    fn isatty(__fd: i32) -> i32;
    fn __b64_ntop(_: *const u8, _: size_t, _: *mut i8, _: size_t) -> i32;
    fn free(_: *mut ());
    fn abs(_: i32) -> i32;
    fn memcpy(_: *mut (), _: *const (), _: u64) -> *mut ();
    fn memset(_: *mut (), _: i32, _: u64) -> *mut ();
    fn strcmp(_: *const i8, _: *const i8) -> i32;
    fn strncmp(_: *const i8, _: *const i8, _: u64) -> i32;
    fn strlen(_: *const i8) -> u64;
    fn strerror(_: i32) -> *mut i8;
    fn tcgetattr(__fd: i32, __termios_p: *mut termios) -> i32;
    fn tcsetattr(__fd: i32, __optional_actions: i32, __termios_p: *const termios) -> i32;
    fn tcflush(__fd: i32, __queue_selector: i32) -> i32;
    fn time(__timer: *mut time_t) -> time_t;
    static mut global_options: *mut options;
    fn setblocking(_: i32, _: i32);
    fn format_create(_: *mut client, _: *mut cmdq_item, _: i32, _: i32) -> *mut format_tree;
    fn format_free(_: *mut format_tree);
    fn format_defaults(
        _: *mut format_tree,
        _: *mut client,
        _: *mut session,
        _: *mut winlink,
        _: *mut window_pane,
    );
    fn options_get_string(_: *mut options, _: *const i8) -> *const i8;
    fn options_get_number(_: *mut options, _: *const i8) -> i64;
    fn tty_term_apply_overrides(_: *mut tty_term);
    fn tty_term_create(
        _: *mut tty,
        _: *mut i8,
        _: *mut *mut i8,
        _: u_int,
        _: *mut i32,
        _: *mut *mut i8,
    ) -> *mut tty_term;
    fn tty_term_free(_: *mut tty_term);
    fn tty_term_has(_: *mut tty_term, _: tty_code_code) -> i32;
    fn tty_term_string(_: *mut tty_term, _: tty_code_code) -> *const i8;
    fn tty_term_string_i(_: *mut tty_term, _: tty_code_code, _: i32) -> *const i8;
    fn tty_term_string_ii(_: *mut tty_term, _: tty_code_code, _: i32, _: i32) -> *const i8;
    fn tty_term_string_iii(_: *mut tty_term, _: tty_code_code, _: i32, _: i32, _: i32)
        -> *const i8;
    fn tty_term_string_s(_: *mut tty_term, _: tty_code_code, _: *const i8) -> *const i8;
    fn tty_term_string_ss(
        _: *mut tty_term,
        _: tty_code_code,
        _: *const i8,
        _: *const i8,
    ) -> *const i8;
    fn tty_term_number(_: *mut tty_term, _: tty_code_code) -> i32;
    fn tty_term_flag(_: *mut tty_term, _: tty_code_code) -> i32;
    fn tty_apply_features(_: *mut tty_term, _: i32) -> i32;
    fn tty_acs_needed(_: *mut tty) -> i32;
    fn tty_acs_get(_: *mut tty, _: u_char) -> *const i8;
    fn tty_acs_reverse_get(_: *mut tty, _: *const i8, _: size_t) -> i32;
    fn tty_keys_build(_: *mut tty);
    fn tty_keys_free(_: *mut tty);
    fn tty_keys_next(_: *mut tty) -> i32;
    static mut clients: clients;
    fn server_client_lost(_: *mut client);
    fn server_client_get_pane(_: *mut client) -> *mut window_pane;
    fn server_redraw_client(_: *mut client);
    fn status_line_size(_: *mut client) -> u_int;
    fn colour_find_rgb(_: u_char, _: u_char, _: u_char) -> i32;
    fn colour_split_rgb(_: i32, _: *mut u_char, _: *mut u_char, _: *mut u_char);
    fn colour_force_rgb(_: i32) -> i32;
    fn colour_256to16(_: i32) -> i32;
    fn colour_palette_get(_: *mut colour_palette, _: i32) -> i32;
    static grid_default_cell: grid_cell;
    fn grid_cells_equal(_: *const grid_cell, _: *const grid_cell) -> i32;
    fn grid_get_line(_: *mut grid, _: u_int) -> *mut grid_line;
    fn fatal(_: *const i8, _: ...) -> !;
    fn log_debug(_: *const i8, _: ...);
    fn hyperlinks_get(
        _: *mut hyperlinks,
        _: u_int,
        _: *mut *const i8,
        _: *mut *const i8,
        _: *mut *const i8,
    ) -> i32;
    fn xsnprintf(_: *mut i8, _: size_t, _: *const i8, _: ...) -> i32;
    fn xmalloc(_: size_t) -> *mut ();
    fn utf8_set(_: *mut utf8_data, _: u_char);
    fn screen_mode_to_string(_: i32) -> *const i8;
    fn log_get_level() -> i32;
    fn screen_select_cell(_: *mut screen, _: *mut grid_cell, _: *const grid_cell);
    fn grid_view_get_cell(_: *mut grid, _: u_int, _: u_int, _: *mut grid_cell);
    fn fatalx(_: *const i8, _: ...) -> !;
    fn style_add(_: *mut grid_cell, _: *mut options, _: *const i8, _: *mut format_tree);
    fn evbuffer_read(buffer: *mut evbuffer, fd: i32, howmuch: i32) -> i32;
    fn evbuffer_write(buffer: *mut evbuffer, fd: i32) -> i32;
    fn evbuffer_drain(buf: *mut evbuffer, len: size_t) -> i32;
    fn evbuffer_add(buf: *mut evbuffer, data: *const (), datlen: size_t) -> i32;
    fn evbuffer_get_length(buf: *const evbuffer) -> size_t;
    fn evbuffer_free(buf: *mut evbuffer);
    fn evbuffer_new() -> *mut evbuffer;
    fn event_set(
        _: *mut event,
        _: i32,
        _: i16,
        _: Option<unsafe extern "C" fn(i32, i16, *mut ()) -> ()>,
        _: *mut (),
    );
    fn event_initialized(ev: *const event) -> i32;
    fn event_del(_: *mut event) -> i32;
    fn event_add(ev: *mut event, timeout: *const timeval) -> i32;
}
pub type __u_char = u8;
pub type __u_short = u16;
pub type __u_int = u32;
pub type __uint8_t = u8;
pub type __uint64_t = u64;
pub type __pid_t = i32;
pub type __time_t = i64;
pub type __useconds_t = u32;
pub type __suseconds_t = i64;
pub type __ssize_t = i64;
pub type u_char = __u_char;
pub type u_short = __u_short;
pub type u_int = __u_int;
pub type pid_t = __pid_t;
pub type ssize_t = __ssize_t;
pub type time_t = __time_t;
pub type size_t = u64;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct timeval {
    pub tv_sec: __time_t,
    pub tv_usec: __suseconds_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winsize {
    pub ws_row: u16,
    pub ws_col: u16,
    pub ws_xpixel: u16,
    pub ws_ypixel: u16,
}
pub type uint8_t = __uint8_t;
pub type uint64_t = __uint64_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen {
    pub title: *mut i8,
    pub path: *mut i8,
    pub titles: *mut screen_titles,
    pub grid: *mut grid,
    pub cx: u_int,
    pub cy: u_int,
    pub cstyle: screen_cursor_style,
    pub default_cstyle: screen_cursor_style,
    pub ccolour: i32,
    pub default_ccolour: i32,
    pub rupper: u_int,
    pub rlower: u_int,
    pub mode: i32,
    pub default_mode: i32,
    pub saved_cx: u_int,
    pub saved_cy: u_int,
    pub saved_grid: *mut grid,
    pub saved_cell: grid_cell,
    pub saved_flags: i32,
    pub tabs: *mut bitstr_t,
    pub sel: *mut screen_sel,
    pub write_list: *mut screen_write_cline,
    pub hyperlinks: *mut hyperlinks,
}
pub type bitstr_t = u8;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_cell {
    pub data: utf8_data,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: i32,
    pub bg: i32,
    pub us: i32,
    pub link: u_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct utf8_data {
    pub data: [u_char; 21],
    pub have: u_char,
    pub size: u_char,
    pub width: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid {
    pub flags: i32,
    pub sx: u_int,
    pub sy: u_int,
    pub hscrolled: u_int,
    pub hsize: u_int,
    pub hlimit: u_int,
    pub linedata: *mut grid_line,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_line {
    pub celldata: *mut grid_cell_entry,
    pub cellused: u_int,
    pub cellsize: u_int,
    pub extddata: *mut grid_extd_entry,
    pub extdsize: u_int,
    pub flags: i32,
    pub time: time_t,
}
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_extd_entry {
    pub data: utf8_char,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: i32,
    pub bg: i32,
    pub us: i32,
    pub link: u_int,
}
pub type utf8_char = u_int;
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_cell_entry {
    pub c2rust_unnamed: C2RustUnnamed,
    pub flags: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed {
    pub offset: u_int,
    pub data: C2RustUnnamed_0,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_0 {
    pub attr: u_char,
    pub fg: u_char,
    pub bg: u_char,
    pub data: u_char,
}
pub type screen_cursor_style = u32;
pub const SCREEN_CURSOR_BAR: screen_cursor_style = 3;
pub const SCREEN_CURSOR_UNDERLINE: screen_cursor_style = 2;
pub const SCREEN_CURSOR_BLOCK: screen_cursor_style = 1;
pub const SCREEN_CURSOR_DEFAULT: screen_cursor_style = 0;
pub type cc_t = u8;
pub type speed_t = u32;
pub type tcflag_t = u32;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct termios {
    pub c_iflag: tcflag_t,
    pub c_oflag: tcflag_t,
    pub c_cflag: tcflag_t,
    pub c_lflag: tcflag_t,
    pub c_line: cc_t,
    pub c_cc: [cc_t; 32],
    pub c_ispeed: speed_t,
    pub c_ospeed: speed_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct event {
    pub ev_evcallback: event_callback,
    pub ev_timeout_pos: C2RustUnnamed_6,
    pub ev_fd: i32,
    pub ev_base: *mut event_base,
    pub ev_: C2RustUnnamed_1,
    pub ev_events: i16,
    pub ev_res: i16,
    pub ev_timeout: timeval,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_1 {
    pub ev_io: C2RustUnnamed_4,
    pub ev_signal: C2RustUnnamed_2,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_2 {
    pub ev_signal_next: C2RustUnnamed_3,
    pub ev_ncalls: i16,
    pub ev_pncalls: *mut i16,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_3 {
    pub le_next: *mut event,
    pub le_prev: *mut *mut event,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_4 {
    pub ev_io_next: C2RustUnnamed_5,
    pub ev_timeout: timeval,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_5 {
    pub le_next: *mut event,
    pub le_prev: *mut *mut event,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_6 {
    pub ev_next_with_common_timeout: C2RustUnnamed_7,
    pub min_heap_idx: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_7 {
    pub tqe_next: *mut event,
    pub tqe_prev: *mut *mut event,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct event_callback {
    pub evcb_active_next: C2RustUnnamed_9,
    pub evcb_flags: i16,
    pub evcb_pri: uint8_t,
    pub evcb_closure: uint8_t,
    pub evcb_cb_union: C2RustUnnamed_8,
    pub evcb_arg: *mut (),
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_8 {
    pub evcb_callback: Option<unsafe extern "C" fn(i32, i16, *mut ()) -> ()>,
    pub evcb_selfcb: Option<unsafe extern "C" fn(*mut event_callback, *mut ()) -> ()>,
    pub evcb_evfinalize: Option<unsafe extern "C" fn(*mut event, *mut ()) -> ()>,
    pub evcb_cbfinalize: Option<unsafe extern "C" fn(*mut event_callback, *mut ()) -> ()>,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_9 {
    pub tqe_next: *mut event_callback,
    pub tqe_prev: *mut *mut event_callback,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct bufferevent {
    pub ev_base: *mut event_base,
    pub be_ops: *const bufferevent_ops,
    pub ev_read: event,
    pub ev_write: event,
    pub input: *mut evbuffer,
    pub output: *mut evbuffer,
    pub wm_read: event_watermark,
    pub wm_write: event_watermark,
    pub readcb: bufferevent_data_cb,
    pub writecb: bufferevent_data_cb,
    pub errorcb: bufferevent_event_cb,
    pub cbarg: *mut (),
    pub timeout_read: timeval,
    pub timeout_write: timeval,
    pub enabled: i16,
}
pub type bufferevent_event_cb =
    Option<unsafe extern "C" fn(*mut bufferevent, i16, *mut ()) -> ()>;
pub type bufferevent_data_cb =
    Option<unsafe extern "C" fn(*mut bufferevent, *mut ()) -> ()>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct event_watermark {
    pub low: size_t,
    pub high: size_t,
}
pub type msgtype = u32;
pub const MSG_READ_CANCEL: msgtype = 307;
pub const MSG_WRITE_CLOSE: msgtype = 306;
pub const MSG_WRITE_READY: msgtype = 305;
pub const MSG_WRITE: msgtype = 304;
pub const MSG_WRITE_OPEN: msgtype = 303;
pub const MSG_READ_DONE: msgtype = 302;
pub const MSG_READ: msgtype = 301;
pub const MSG_READ_OPEN: msgtype = 300;
pub const MSG_FLAGS: msgtype = 218;
pub const MSG_EXEC: msgtype = 217;
pub const MSG_WAKEUP: msgtype = 216;
pub const MSG_UNLOCK: msgtype = 215;
pub const MSG_SUSPEND: msgtype = 214;
pub const MSG_OLDSTDOUT: msgtype = 213;
pub const MSG_OLDSTDIN: msgtype = 212;
pub const MSG_OLDSTDERR: msgtype = 211;
pub const MSG_SHUTDOWN: msgtype = 210;
pub const MSG_SHELL: msgtype = 209;
pub const MSG_RESIZE: msgtype = 208;
pub const MSG_READY: msgtype = 207;
pub const MSG_LOCK: msgtype = 206;
pub const MSG_EXITING: msgtype = 205;
pub const MSG_EXITED: msgtype = 204;
pub const MSG_EXIT: msgtype = 203;
pub const MSG_DETACHKILL: msgtype = 202;
pub const MSG_DETACH: msgtype = 201;
pub const MSG_COMMAND: msgtype = 200;
pub const MSG_IDENTIFY_TERMINFO: msgtype = 112;
pub const MSG_IDENTIFY_LONGFLAGS: msgtype = 111;
pub const MSG_IDENTIFY_STDOUT: msgtype = 110;
pub const MSG_IDENTIFY_FEATURES: msgtype = 109;
pub const MSG_IDENTIFY_CWD: msgtype = 108;
pub const MSG_IDENTIFY_CLIENTPID: msgtype = 107;
pub const MSG_IDENTIFY_DONE: msgtype = 106;
pub const MSG_IDENTIFY_ENVIRON: msgtype = 105;
pub const MSG_IDENTIFY_STDIN: msgtype = 104;
pub const MSG_IDENTIFY_OLDCWD: msgtype = 103;
pub const MSG_IDENTIFY_TTYNAME: msgtype = 102;
pub const MSG_IDENTIFY_TERM: msgtype = 101;
pub const MSG_IDENTIFY_FLAGS: msgtype = 100;
pub const MSG_VERSION: msgtype = 12;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client {
    pub name: *const i8,
    pub peer: *mut tmuxpeer,
    pub queue: *mut cmdq_list,
    pub windows: client_windows,
    pub control_state: *mut control_state,
    pub pause_age: u_int,
    pub pid: pid_t,
    pub fd: i32,
    pub out_fd: i32,
    pub event: event,
    pub retval: i32,
    pub creation_time: timeval,
    pub activity_time: timeval,
    pub last_activity_time: timeval,
    pub environ: *mut environ,
    pub jobs: *mut format_job_tree,
    pub title: *mut i8,
    pub path: *mut i8,
    pub cwd: *const i8,
    pub term_name: *mut i8,
    pub term_features: i32,
    pub term_type: *mut i8,
    pub term_caps: *mut *mut i8,
    pub term_ncaps: u_int,
    pub ttyname: *mut i8,
    pub tty: tty,
    pub written: size_t,
    pub discarded: size_t,
    pub redraw: size_t,
    pub repeat_timer: event,
    pub click_timer: event,
    pub click_button: u_int,
    pub click_event: mouse_event,
    pub status: status_line,
    pub flags: uint64_t,
    pub exit_type: C2RustUnnamed_30,
    pub exit_msgtype: msgtype,
    pub exit_session: *mut i8,
    pub exit_message: *mut i8,
    pub keytable: *mut key_table,
    pub last_key: key_code,
    pub redraw_panes: uint64_t,
    pub redraw_scrollbars: uint64_t,
    pub message_ignore_keys: i32,
    pub message_ignore_styles: i32,
    pub message_string: *mut i8,
    pub message_timer: event,
    pub prompt_string: *mut i8,
    pub prompt_buffer: *mut utf8_data,
    pub prompt_last: *mut i8,
    pub prompt_index: size_t,
    pub prompt_inputcb: prompt_input_cb,
    pub prompt_freecb: prompt_free_cb,
    pub prompt_data: *mut (),
    pub prompt_hindex: [u_int; 4],
    pub prompt_mode: C2RustUnnamed_27,
    pub prompt_saved: *mut utf8_data,
    pub prompt_flags: i32,
    pub prompt_type: prompt_type,
    pub prompt_cursor: i32,
    pub session: *mut session,
    pub last_session: *mut session,
    pub references: i32,
    pub pan_window: *mut (),
    pub pan_ox: u_int,
    pub pan_oy: u_int,
    pub overlay_check: overlay_check_cb,
    pub overlay_mode: overlay_mode_cb,
    pub overlay_draw: overlay_draw_cb,
    pub overlay_key: overlay_key_cb,
    pub overlay_free: overlay_free_cb,
    pub overlay_resize: overlay_resize_cb,
    pub overlay_data: *mut (),
    pub overlay_timer: event,
    pub files: client_files,
    pub clipboard_panes: *mut u_int,
    pub clipboard_npanes: u_int,
    pub entry: C2RustUnnamed_10,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_10 {
    pub tqe_next: *mut client,
    pub tqe_prev: *mut *mut client,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_files {
    pub rbh_root: *mut client_file,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_file {
    pub c: *mut client,
    pub peer: *mut tmuxpeer,
    pub tree: *mut client_files,
    pub references: i32,
    pub stream: i32,
    pub path: *mut i8,
    pub buffer: *mut evbuffer,
    pub event: *mut bufferevent,
    pub fd: i32,
    pub error: i32,
    pub closed: i32,
    pub cb: client_file_cb,
    pub data: *mut (),
    pub entry: C2RustUnnamed_11,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_11 {
    pub rbe_left: *mut client_file,
    pub rbe_right: *mut client_file,
    pub rbe_parent: *mut client_file,
    pub rbe_color: i32,
}
pub type client_file_cb = Option<
    unsafe extern "C" fn(*mut client, *const i8, i32, i32, *mut evbuffer, *mut ()) -> (),
>;
pub type overlay_resize_cb = Option<unsafe extern "C" fn(*mut client, *mut ()) -> ()>;
pub type overlay_free_cb = Option<unsafe extern "C" fn(*mut client, *mut ()) -> ()>;
pub type overlay_key_cb =
    Option<unsafe extern "C" fn(*mut client, *mut (), *mut key_event) -> i32>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_event {
    pub key: key_code,
    pub m: mouse_event,
    pub buf: *mut i8,
    pub len: size_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct mouse_event {
    pub valid: i32,
    pub ignore: i32,
    pub key: key_code,
    pub statusat: i32,
    pub statuslines: u_int,
    pub x: u_int,
    pub y: u_int,
    pub b: u_int,
    pub lx: u_int,
    pub ly: u_int,
    pub lb: u_int,
    pub ox: u_int,
    pub oy: u_int,
    pub s: i32,
    pub w: i32,
    pub wp: i32,
    pub sgr_type: u_int,
    pub sgr_b: u_int,
}
pub type key_code = u64;
pub type overlay_draw_cb =
    Option<unsafe extern "C" fn(*mut client, *mut (), *mut screen_redraw_ctx) -> ()>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen_redraw_ctx {
    pub c: *mut client,
    pub statuslines: u_int,
    pub statustop: i32,
    pub pane_status: i32,
    pub pane_lines: pane_lines,
    pub pane_scrollbars: i32,
    pub pane_scrollbars_pos: i32,
    pub no_pane_gc: grid_cell,
    pub no_pane_gc_set: i32,
    pub sx: u_int,
    pub sy: u_int,
    pub ox: u_int,
    pub oy: u_int,
}
pub type pane_lines = u32;
pub const PANE_LINES_NUMBER: pane_lines = 4;
pub const PANE_LINES_SIMPLE: pane_lines = 3;
pub const PANE_LINES_HEAVY: pane_lines = 2;
pub const PANE_LINES_DOUBLE: pane_lines = 1;
pub const PANE_LINES_SINGLE: pane_lines = 0;
pub type overlay_mode_cb = Option<
    unsafe extern "C" fn(*mut client, *mut (), *mut u_int, *mut u_int) -> *mut screen,
>;
pub type overlay_check_cb = Option<
    unsafe extern "C" fn(
        *mut client,
        *mut (),
        u_int,
        u_int,
        u_int,
        *mut overlay_ranges,
    ) -> (),
>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct overlay_ranges {
    pub px: [u_int; 3],
    pub nx: [u_int; 3],
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct session {
    pub id: u_int,
    pub name: *mut i8,
    pub cwd: *const i8,
    pub creation_time: timeval,
    pub last_attached_time: timeval,
    pub activity_time: timeval,
    pub last_activity_time: timeval,
    pub lock_timer: event,
    pub curw: *mut winlink,
    pub lastw: winlink_stack,
    pub windows: winlinks,
    pub statusat: i32,
    pub statuslines: u_int,
    pub options: *mut options,
    pub flags: i32,
    pub attached: u_int,
    pub tio: *mut termios,
    pub environ: *mut environ,
    pub references: i32,
    pub gentry: C2RustUnnamed_13,
    pub entry: C2RustUnnamed_12,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_12 {
    pub rbe_left: *mut session,
    pub rbe_right: *mut session,
    pub rbe_parent: *mut session,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_13 {
    pub tqe_next: *mut session,
    pub tqe_prev: *mut *mut session,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winlinks {
    pub rbh_root: *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winlink {
    pub idx: i32,
    pub session: *mut session,
    pub window: *mut window,
    pub flags: i32,
    pub entry: C2RustUnnamed_16,
    pub wentry: C2RustUnnamed_15,
    pub sentry: C2RustUnnamed_14,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_14 {
    pub tqe_next: *mut winlink,
    pub tqe_prev: *mut *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_15 {
    pub tqe_next: *mut winlink,
    pub tqe_prev: *mut *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_16 {
    pub rbe_left: *mut winlink,
    pub rbe_right: *mut winlink,
    pub rbe_parent: *mut winlink,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window {
    pub id: u_int,
    pub latest: *mut (),
    pub name: *mut i8,
    pub name_event: event,
    pub name_time: timeval,
    pub alerts_timer: event,
    pub offset_timer: event,
    pub activity_time: timeval,
    pub active: *mut window_pane,
    pub last_panes: window_panes,
    pub panes: window_panes,
    pub lastlayout: i32,
    pub layout_root: *mut layout_cell,
    pub saved_layout_root: *mut layout_cell,
    pub old_layout: *mut i8,
    pub sx: u_int,
    pub sy: u_int,
    pub manual_sx: u_int,
    pub manual_sy: u_int,
    pub xpixel: u_int,
    pub ypixel: u_int,
    pub new_sx: u_int,
    pub new_sy: u_int,
    pub new_xpixel: u_int,
    pub new_ypixel: u_int,
    pub fill_character: *mut utf8_data,
    pub flags: i32,
    pub alerts_queued: i32,
    pub alerts_entry: C2RustUnnamed_19,
    pub options: *mut options,
    pub references: u_int,
    pub winlinks: C2RustUnnamed_18,
    pub entry: C2RustUnnamed_17,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_17 {
    pub rbe_left: *mut window,
    pub rbe_right: *mut window,
    pub rbe_parent: *mut window,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_18 {
    pub tqh_first: *mut winlink,
    pub tqh_last: *mut *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_19 {
    pub tqe_next: *mut window,
    pub tqe_prev: *mut *mut window,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct layout_cell {
    pub type_0: layout_type,
    pub parent: *mut layout_cell,
    pub sx: u_int,
    pub sy: u_int,
    pub xoff: u_int,
    pub yoff: u_int,
    pub wp: *mut window_pane,
    pub cells: layout_cells,
    pub entry: C2RustUnnamed_20,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_20 {
    pub tqe_next: *mut layout_cell,
    pub tqe_prev: *mut *mut layout_cell,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct layout_cells {
    pub tqh_first: *mut layout_cell,
    pub tqh_last: *mut *mut layout_cell,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane {
    pub id: u_int,
    pub active_point: u_int,
    pub window: *mut window,
    pub options: *mut options,
    pub layout_cell: *mut layout_cell,
    pub saved_layout_cell: *mut layout_cell,
    pub sx: u_int,
    pub sy: u_int,
    pub xoff: u_int,
    pub yoff: u_int,
    pub flags: i32,
    pub sb_slider_y: u_int,
    pub sb_slider_h: u_int,
    pub argc: i32,
    pub argv: *mut *mut i8,
    pub shell: *mut i8,
    pub cwd: *mut i8,
    pub pid: pid_t,
    pub tty: [i8; 32],
    pub status: i32,
    pub dead_time: timeval,
    pub fd: i32,
    pub event: *mut bufferevent,
    pub offset: window_pane_offset,
    pub base_offset: size_t,
    pub resize_queue: window_pane_resizes,
    pub resize_timer: event,
    pub ictx: *mut input_ctx,
    pub cached_gc: grid_cell,
    pub cached_active_gc: grid_cell,
    pub palette: colour_palette,
    pub pipe_fd: i32,
    pub pipe_event: *mut bufferevent,
    pub pipe_offset: window_pane_offset,
    pub screen: *mut screen,
    pub base: screen,
    pub status_screen: screen,
    pub status_size: size_t,
    pub modes: C2RustUnnamed_24,
    pub searchstr: *mut i8,
    pub searchregex: i32,
    pub border_gc_set: i32,
    pub border_gc: grid_cell,
    pub control_bg: i32,
    pub control_fg: i32,
    pub scrollbar_style: style,
    pub entry: C2RustUnnamed_23,
    pub sentry: C2RustUnnamed_22,
    pub tree_entry: C2RustUnnamed_21,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_21 {
    pub rbe_left: *mut window_pane,
    pub rbe_right: *mut window_pane,
    pub rbe_parent: *mut window_pane,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_22 {
    pub tqe_next: *mut window_pane,
    pub tqe_prev: *mut *mut window_pane,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_23 {
    pub tqe_next: *mut window_pane,
    pub tqe_prev: *mut *mut window_pane,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct style {
    pub gc: grid_cell,
    pub ignore: i32,
    pub fill: i32,
    pub align: style_align,
    pub list: style_list,
    pub range_type: style_range_type,
    pub range_argument: u_int,
    pub range_string: [i8; 16],
    pub width: i32,
    pub pad: i32,
    pub default_type: style_default_type,
}
pub type style_default_type = u32;
pub const STYLE_DEFAULT_POP: style_default_type = 2;
pub const STYLE_DEFAULT_PUSH: style_default_type = 1;
pub const STYLE_DEFAULT_BASE: style_default_type = 0;
pub type style_range_type = u32;
pub const STYLE_RANGE_USER: style_range_type = 6;
pub const STYLE_RANGE_SESSION: style_range_type = 5;
pub const STYLE_RANGE_WINDOW: style_range_type = 4;
pub const STYLE_RANGE_PANE: style_range_type = 3;
pub const STYLE_RANGE_RIGHT: style_range_type = 2;
pub const STYLE_RANGE_LEFT: style_range_type = 1;
pub const STYLE_RANGE_NONE: style_range_type = 0;
pub type style_list = u32;
pub const STYLE_LIST_RIGHT_MARKER: style_list = 4;
pub const STYLE_LIST_LEFT_MARKER: style_list = 3;
pub const STYLE_LIST_FOCUS: style_list = 2;
pub const STYLE_LIST_ON: style_list = 1;
pub const STYLE_LIST_OFF: style_list = 0;
pub type style_align = u32;
pub const STYLE_ALIGN_ABSOLUTE_CENTRE: style_align = 4;
pub const STYLE_ALIGN_RIGHT: style_align = 3;
pub const STYLE_ALIGN_CENTRE: style_align = 2;
pub const STYLE_ALIGN_LEFT: style_align = 1;
pub const STYLE_ALIGN_DEFAULT: style_align = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_24 {
    pub tqh_first: *mut window_mode_entry,
    pub tqh_last: *mut *mut window_mode_entry,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_mode_entry {
    pub wp: *mut window_pane,
    pub swp: *mut window_pane,
    pub mode: *const window_mode,
    pub data: *mut (),
    pub screen: *mut screen,
    pub prefix: u_int,
    pub entry: C2RustUnnamed_25,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_25 {
    pub tqe_next: *mut window_mode_entry,
    pub tqe_prev: *mut *mut window_mode_entry,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_mode {
    pub name: *const i8,
    pub default_format: *const i8,
    pub init: Option<
        unsafe extern "C" fn(*mut window_mode_entry, *mut cmd_find_state, *mut args) -> *mut screen,
    >,
    pub free: Option<unsafe extern "C" fn(*mut window_mode_entry) -> ()>,
    pub resize: Option<unsafe extern "C" fn(*mut window_mode_entry, u_int, u_int) -> ()>,
    pub update: Option<unsafe extern "C" fn(*mut window_mode_entry) -> ()>,
    pub key: Option<
        unsafe extern "C" fn(
            *mut window_mode_entry,
            *mut client,
            *mut session,
            *mut winlink,
            key_code,
            *mut mouse_event,
        ) -> (),
    >,
    pub key_table: Option<unsafe extern "C" fn(*mut window_mode_entry) -> *const i8>,
    pub command: Option<
        unsafe extern "C" fn(
            *mut window_mode_entry,
            *mut client,
            *mut session,
            *mut winlink,
            *mut args,
            *mut mouse_event,
        ) -> (),
    >,
    pub formats: Option<unsafe extern "C" fn(*mut window_mode_entry, *mut format_tree) -> ()>,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct cmd_find_state {
    pub flags: i32,
    pub current: *mut cmd_find_state,
    pub s: *mut session,
    pub wl: *mut winlink,
    pub w: *mut window,
    pub wp: *mut window_pane,
    pub idx: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane_offset {
    pub used: size_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct colour_palette {
    pub fg: i32,
    pub bg: i32,
    pub palette: *mut i32,
    pub default_palette: *mut i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane_resizes {
    pub tqh_first: *mut window_pane_resize,
    pub tqh_last: *mut *mut window_pane_resize,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane_resize {
    pub sx: u_int,
    pub sy: u_int,
    pub osx: u_int,
    pub osy: u_int,
    pub entry: C2RustUnnamed_26,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_26 {
    pub tqe_next: *mut window_pane_resize,
    pub tqe_prev: *mut *mut window_pane_resize,
}
pub type layout_type = u32;
pub const LAYOUT_WINDOWPANE: layout_type = 2;
pub const LAYOUT_TOPBOTTOM: layout_type = 1;
pub const LAYOUT_LEFTRIGHT: layout_type = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_panes {
    pub tqh_first: *mut window_pane,
    pub tqh_last: *mut *mut window_pane,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winlink_stack {
    pub tqh_first: *mut winlink,
    pub tqh_last: *mut *mut winlink,
}
pub type prompt_type = u32;
pub const PROMPT_TYPE_INVALID: prompt_type = 255;
pub const PROMPT_TYPE_WINDOW_TARGET: prompt_type = 3;
pub const PROMPT_TYPE_TARGET: prompt_type = 2;
pub const PROMPT_TYPE_SEARCH: prompt_type = 1;
pub const PROMPT_TYPE_COMMAND: prompt_type = 0;
pub type C2RustUnnamed_27 = u32;
pub const PROMPT_COMMAND: C2RustUnnamed_27 = 1;
pub const PROMPT_ENTRY: C2RustUnnamed_27 = 0;
pub type prompt_free_cb = Option<unsafe extern "C" fn(*mut ()) -> ()>;
pub type prompt_input_cb =
    Option<unsafe extern "C" fn(*mut client, *mut (), *const i8, i32) -> i32>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_table {
    pub name: *const i8,
    pub activity_time: timeval,
    pub key_bindings: key_bindings,
    pub default_key_bindings: key_bindings,
    pub references: u_int,
    pub entry: C2RustUnnamed_28,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_28 {
    pub rbe_left: *mut key_table,
    pub rbe_right: *mut key_table,
    pub rbe_parent: *mut key_table,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_bindings {
    pub rbh_root: *mut key_binding,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_binding {
    pub key: key_code,
    pub cmdlist: *mut cmd_list,
    pub note: *const i8,
    pub flags: i32,
    pub entry: C2RustUnnamed_29,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_29 {
    pub rbe_left: *mut key_binding,
    pub rbe_right: *mut key_binding,
    pub rbe_parent: *mut key_binding,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct cmd_list {
    pub references: i32,
    pub group: u_int,
    pub list: *mut cmds,
}
pub type C2RustUnnamed_30 = u32;
pub const CLIENT_EXIT_DETACH: C2RustUnnamed_30 = 2;
pub const CLIENT_EXIT_SHUTDOWN: C2RustUnnamed_30 = 1;
pub const CLIENT_EXIT_RETURN: C2RustUnnamed_30 = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct status_line {
    pub timer: event,
    pub screen: screen,
    pub active: *mut screen,
    pub references: i32,
    pub style: grid_cell,
    pub entries: [status_line_entry; 5],
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct status_line_entry {
    pub expanded: *mut i8,
    pub ranges: style_ranges,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct style_ranges {
    pub tqh_first: *mut style_range,
    pub tqh_last: *mut *mut style_range,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct style_range {
    pub type_0: style_range_type,
    pub argument: u_int,
    pub string: [i8; 16],
    pub start: u_int,
    pub end: u_int,
    pub entry: C2RustUnnamed_31,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_31 {
    pub tqe_next: *mut style_range,
    pub tqe_prev: *mut *mut style_range,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct tty {
    pub client: *mut client,
    pub start_timer: event,
    pub clipboard_timer: event,
    pub last_requests: time_t,
    pub sx: u_int,
    pub sy: u_int,
    pub xpixel: u_int,
    pub ypixel: u_int,
    pub cx: u_int,
    pub cy: u_int,
    pub cstyle: screen_cursor_style,
    pub ccolour: i32,
    pub oflag: i32,
    pub oox: u_int,
    pub ooy: u_int,
    pub osx: u_int,
    pub osy: u_int,
    pub mode: i32,
    pub fg: i32,
    pub bg: i32,
    pub rlower: u_int,
    pub rupper: u_int,
    pub rleft: u_int,
    pub rright: u_int,
    pub event_in: event,
    pub in_0: *mut evbuffer,
    pub event_out: event,
    pub out: *mut evbuffer,
    pub timer: event,
    pub discarded: size_t,
    pub tio: termios,
    pub cell: grid_cell,
    pub last_cell: grid_cell,
    pub flags: i32,
    pub term: *mut tty_term,
    pub mouse_last_x: u_int,
    pub mouse_last_y: u_int,
    pub mouse_last_b: u_int,
    pub mouse_drag_flag: i32,
    pub mouse_scrolling_flag: i32,
    pub mouse_slider_mpos: i32,
    pub mouse_drag_update: Option<unsafe extern "C" fn(*mut client, *mut mouse_event) -> ()>,
    pub mouse_drag_release: Option<unsafe extern "C" fn(*mut client, *mut mouse_event) -> ()>,
    pub key_timer: event,
    pub key_tree: *mut tty_key,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct tty_term {
    pub name: *mut i8,
    pub tty: *mut tty,
    pub features: i32,
    pub acs: [[i8; 2]; 256],
    pub codes: *mut tty_code,
    pub flags: i32,
    pub entry: C2RustUnnamed_32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_32 {
    pub le_next: *mut tty_term,
    pub le_prev: *mut *mut tty_term,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_windows {
    pub rbh_root: *mut client_window,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_window {
    pub window: u_int,
    pub pane: *mut window_pane,
    pub sx: u_int,
    pub sy: u_int,
    pub entry: C2RustUnnamed_33,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_33 {
    pub rbe_left: *mut client_window,
    pub rbe_right: *mut client_window,
    pub rbe_parent: *mut client_window,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct tty_ctx {
    pub s: *mut screen,
    pub redraw_cb: tty_ctx_redraw_cb,
    pub set_client_cb: tty_ctx_set_client_cb,
    pub arg: *mut (),
    pub cell: *const grid_cell,
    pub wrapped: i32,
    pub num: u_int,
    pub ptr: *mut (),
    pub ptr2: *mut (),
    pub allow_invisible_panes: i32,
    pub ocx: u_int,
    pub ocy: u_int,
    pub orupper: u_int,
    pub orlower: u_int,
    pub xoff: u_int,
    pub yoff: u_int,
    pub rxoff: u_int,
    pub ryoff: u_int,
    pub sx: u_int,
    pub sy: u_int,
    pub bg: u_int,
    pub defaults: grid_cell,
    pub palette: *mut colour_palette,
    pub bigger: i32,
    pub wox: u_int,
    pub woy: u_int,
    pub wsx: u_int,
    pub wsy: u_int,
}
pub type tty_ctx_set_client_cb = Option<unsafe extern "C" fn(*mut tty_ctx, *mut client) -> i32>;
pub type tty_ctx_redraw_cb = Option<unsafe extern "C" fn(*const tty_ctx) -> ()>;
pub type tty_code_code = u32;
pub const TTYC_XT: tty_code_code = 231;
pub const TTYC_VPA: tty_code_code = 230;
pub const TTYC_U8: tty_code_code = 229;
pub const TTYC_TSL: tty_code_code = 228;
pub const TTYC_TC: tty_code_code = 227;
pub const TTYC_SYNC: tty_code_code = 226;
pub const TTYC_SWD: tty_code_code = 225;
pub const TTYC_SS: tty_code_code = 224;
pub const TTYC_SXL: tty_code_code = 223;
pub const TTYC_SMXX: tty_code_code = 222;
pub const TTYC_SMULX: tty_code_code = 221;
pub const TTYC_SMUL: tty_code_code = 220;
pub const TTYC_SMSO: tty_code_code = 219;
pub const TTYC_SMOL: tty_code_code = 218;
pub const TTYC_SMKX: tty_code_code = 217;
pub const TTYC_SMCUP: tty_code_code = 216;
pub const TTYC_SMACS: tty_code_code = 215;
pub const TTYC_SITM: tty_code_code = 214;
pub const TTYC_SGR0: tty_code_code = 213;
pub const TTYC_SETULC1: tty_code_code = 212;
pub const TTYC_SETULC: tty_code_code = 211;
pub const TTYC_SETRGBF: tty_code_code = 210;
pub const TTYC_SETRGBB: tty_code_code = 209;
pub const TTYC_SETAL: tty_code_code = 208;
pub const TTYC_SETAF: tty_code_code = 207;
pub const TTYC_SETAB: tty_code_code = 206;
pub const TTYC_SE: tty_code_code = 205;
pub const TTYC_RMKX: tty_code_code = 204;
pub const TTYC_RMCUP: tty_code_code = 203;
pub const TTYC_RMACS: tty_code_code = 202;
pub const TTYC_RIN: tty_code_code = 201;
pub const TTYC_RI: tty_code_code = 200;
pub const TTYC_RGB: tty_code_code = 199;
pub const TTYC_REV: tty_code_code = 198;
pub const TTYC_RECT: tty_code_code = 197;
pub const TTYC_OP: tty_code_code = 196;
pub const TTYC_OL: tty_code_code = 195;
pub const TTYC_NOBR: tty_code_code = 194;
pub const TTYC_MS: tty_code_code = 193;
pub const TTYC_KUP7: tty_code_code = 192;
pub const TTYC_KUP6: tty_code_code = 191;
pub const TTYC_KUP5: tty_code_code = 190;
pub const TTYC_KUP4: tty_code_code = 189;
pub const TTYC_KUP3: tty_code_code = 188;
pub const TTYC_KUP2: tty_code_code = 187;
pub const TTYC_KRIT7: tty_code_code = 186;
pub const TTYC_KRIT6: tty_code_code = 185;
pub const TTYC_KRIT5: tty_code_code = 184;
pub const TTYC_KRIT4: tty_code_code = 183;
pub const TTYC_KRIT3: tty_code_code = 182;
pub const TTYC_KRIT2: tty_code_code = 181;
pub const TTYC_KRI: tty_code_code = 180;
pub const TTYC_KPRV7: tty_code_code = 179;
pub const TTYC_KPRV6: tty_code_code = 178;
pub const TTYC_KPRV5: tty_code_code = 177;
pub const TTYC_KPRV4: tty_code_code = 176;
pub const TTYC_KPRV3: tty_code_code = 175;
pub const TTYC_KPRV2: tty_code_code = 174;
pub const TTYC_KPP: tty_code_code = 173;
pub const TTYC_KNXT7: tty_code_code = 172;
pub const TTYC_KNXT6: tty_code_code = 171;
pub const TTYC_KNXT5: tty_code_code = 170;
pub const TTYC_KNXT4: tty_code_code = 169;
pub const TTYC_KNXT3: tty_code_code = 168;
pub const TTYC_KNXT2: tty_code_code = 167;
pub const TTYC_KNP: tty_code_code = 166;
pub const TTYC_KMOUS: tty_code_code = 165;
pub const TTYC_KLFT7: tty_code_code = 164;
pub const TTYC_KLFT6: tty_code_code = 163;
pub const TTYC_KLFT5: tty_code_code = 162;
pub const TTYC_KLFT4: tty_code_code = 161;
pub const TTYC_KLFT3: tty_code_code = 160;
pub const TTYC_KLFT2: tty_code_code = 159;
pub const TTYC_KIND: tty_code_code = 158;
pub const TTYC_KICH1: tty_code_code = 157;
pub const TTYC_KIC7: tty_code_code = 156;
pub const TTYC_KIC6: tty_code_code = 155;
pub const TTYC_KIC5: tty_code_code = 154;
pub const TTYC_KIC4: tty_code_code = 153;
pub const TTYC_KIC3: tty_code_code = 152;
pub const TTYC_KIC2: tty_code_code = 151;
pub const TTYC_KHOME: tty_code_code = 150;
pub const TTYC_KHOM7: tty_code_code = 149;
pub const TTYC_KHOM6: tty_code_code = 148;
pub const TTYC_KHOM5: tty_code_code = 147;
pub const TTYC_KHOM4: tty_code_code = 146;
pub const TTYC_KHOM3: tty_code_code = 145;
pub const TTYC_KHOM2: tty_code_code = 144;
pub const TTYC_KF9: tty_code_code = 143;
pub const TTYC_KF8: tty_code_code = 142;
pub const TTYC_KF7: tty_code_code = 141;
pub const TTYC_KF63: tty_code_code = 140;
pub const TTYC_KF62: tty_code_code = 139;
pub const TTYC_KF61: tty_code_code = 138;
pub const TTYC_KF60: tty_code_code = 137;
pub const TTYC_KF6: tty_code_code = 136;
pub const TTYC_KF59: tty_code_code = 135;
pub const TTYC_KF58: tty_code_code = 134;
pub const TTYC_KF57: tty_code_code = 133;
pub const TTYC_KF56: tty_code_code = 132;
pub const TTYC_KF55: tty_code_code = 131;
pub const TTYC_KF54: tty_code_code = 130;
pub const TTYC_KF53: tty_code_code = 129;
pub const TTYC_KF52: tty_code_code = 128;
pub const TTYC_KF51: tty_code_code = 127;
pub const TTYC_KF50: tty_code_code = 126;
pub const TTYC_KF5: tty_code_code = 125;
pub const TTYC_KF49: tty_code_code = 124;
pub const TTYC_KF48: tty_code_code = 123;
pub const TTYC_KF47: tty_code_code = 122;
pub const TTYC_KF46: tty_code_code = 121;
pub const TTYC_KF45: tty_code_code = 120;
pub const TTYC_KF44: tty_code_code = 119;
pub const TTYC_KF43: tty_code_code = 118;
pub const TTYC_KF42: tty_code_code = 117;
pub const TTYC_KF41: tty_code_code = 116;
pub const TTYC_KF40: tty_code_code = 115;
pub const TTYC_KF4: tty_code_code = 114;
pub const TTYC_KF39: tty_code_code = 113;
pub const TTYC_KF38: tty_code_code = 112;
pub const TTYC_KF37: tty_code_code = 111;
pub const TTYC_KF36: tty_code_code = 110;
pub const TTYC_KF35: tty_code_code = 109;
pub const TTYC_KF34: tty_code_code = 108;
pub const TTYC_KF33: tty_code_code = 107;
pub const TTYC_KF32: tty_code_code = 106;
pub const TTYC_KF31: tty_code_code = 105;
pub const TTYC_KF30: tty_code_code = 104;
pub const TTYC_KF3: tty_code_code = 103;
pub const TTYC_KF29: tty_code_code = 102;
pub const TTYC_KF28: tty_code_code = 101;
pub const TTYC_KF27: tty_code_code = 100;
pub const TTYC_KF26: tty_code_code = 99;
pub const TTYC_KF25: tty_code_code = 98;
pub const TTYC_KF24: tty_code_code = 97;
pub const TTYC_KF23: tty_code_code = 96;
pub const TTYC_KF22: tty_code_code = 95;
pub const TTYC_KF21: tty_code_code = 94;
pub const TTYC_KF20: tty_code_code = 93;
pub const TTYC_KF2: tty_code_code = 92;
pub const TTYC_KF19: tty_code_code = 91;
pub const TTYC_KF18: tty_code_code = 90;
pub const TTYC_KF17: tty_code_code = 89;
pub const TTYC_KF16: tty_code_code = 88;
pub const TTYC_KF15: tty_code_code = 87;
pub const TTYC_KF14: tty_code_code = 86;
pub const TTYC_KF13: tty_code_code = 85;
pub const TTYC_KF12: tty_code_code = 84;
pub const TTYC_KF11: tty_code_code = 83;
pub const TTYC_KF10: tty_code_code = 82;
pub const TTYC_KF1: tty_code_code = 81;
pub const TTYC_KEND7: tty_code_code = 80;
pub const TTYC_KEND6: tty_code_code = 79;
pub const TTYC_KEND5: tty_code_code = 78;
pub const TTYC_KEND4: tty_code_code = 77;
pub const TTYC_KEND3: tty_code_code = 76;
pub const TTYC_KEND2: tty_code_code = 75;
pub const TTYC_KEND: tty_code_code = 74;
pub const TTYC_KDN7: tty_code_code = 73;
pub const TTYC_KDN6: tty_code_code = 72;
pub const TTYC_KDN5: tty_code_code = 71;
pub const TTYC_KDN4: tty_code_code = 70;
pub const TTYC_KDN3: tty_code_code = 69;
pub const TTYC_KDN2: tty_code_code = 68;
pub const TTYC_KDCH1: tty_code_code = 67;
pub const TTYC_KDC7: tty_code_code = 66;
pub const TTYC_KDC6: tty_code_code = 65;
pub const TTYC_KDC5: tty_code_code = 64;
pub const TTYC_KDC4: tty_code_code = 63;
pub const TTYC_KDC3: tty_code_code = 62;
pub const TTYC_KDC2: tty_code_code = 61;
pub const TTYC_KCUU1: tty_code_code = 60;
pub const TTYC_KCUF1: tty_code_code = 59;
pub const TTYC_KCUD1: tty_code_code = 58;
pub const TTYC_KCUB1: tty_code_code = 57;
pub const TTYC_KCBT: tty_code_code = 56;
pub const TTYC_INVIS: tty_code_code = 55;
pub const TTYC_INDN: tty_code_code = 54;
pub const TTYC_IL1: tty_code_code = 53;
pub const TTYC_IL: tty_code_code = 52;
pub const TTYC_ICH1: tty_code_code = 51;
pub const TTYC_ICH: tty_code_code = 50;
pub const TTYC_HPA: tty_code_code = 49;
pub const TTYC_HOME: tty_code_code = 48;
pub const TTYC_HLS: tty_code_code = 47;
pub const TTYC_FSL: tty_code_code = 46;
pub const TTYC_ENMG: tty_code_code = 45;
pub const TTYC_ENFCS: tty_code_code = 44;
pub const TTYC_ENEKS: tty_code_code = 43;
pub const TTYC_ENBP: tty_code_code = 42;
pub const TTYC_ENACS: tty_code_code = 41;
pub const TTYC_EL1: tty_code_code = 40;
pub const TTYC_EL: tty_code_code = 39;
pub const TTYC_ED: tty_code_code = 38;
pub const TTYC_ECH: tty_code_code = 37;
pub const TTYC_E3: tty_code_code = 36;
pub const TTYC_DSMG: tty_code_code = 35;
pub const TTYC_DSFCS: tty_code_code = 34;
pub const TTYC_DSEKS: tty_code_code = 33;
pub const TTYC_DSBP: tty_code_code = 32;
pub const TTYC_DL1: tty_code_code = 31;
pub const TTYC_DL: tty_code_code = 30;
pub const TTYC_DIM: tty_code_code = 29;
pub const TTYC_DCH1: tty_code_code = 28;
pub const TTYC_DCH: tty_code_code = 27;
pub const TTYC_CVVIS: tty_code_code = 26;
pub const TTYC_CUU1: tty_code_code = 25;
pub const TTYC_CUU: tty_code_code = 24;
pub const TTYC_CUP: tty_code_code = 23;
pub const TTYC_CUF1: tty_code_code = 22;
pub const TTYC_CUF: tty_code_code = 21;
pub const TTYC_CUD1: tty_code_code = 20;
pub const TTYC_CUD: tty_code_code = 19;
pub const TTYC_CUB1: tty_code_code = 18;
pub const TTYC_CUB: tty_code_code = 17;
pub const TTYC_CSR: tty_code_code = 16;
pub const TTYC_CS: tty_code_code = 15;
pub const TTYC_CR: tty_code_code = 14;
pub const TTYC_COLORS: tty_code_code = 13;
pub const TTYC_CNORM: tty_code_code = 12;
pub const TTYC_CMG: tty_code_code = 11;
pub const TTYC_CLMG: tty_code_code = 10;
pub const TTYC_CLEAR: tty_code_code = 9;
pub const TTYC_CIVIS: tty_code_code = 8;
pub const TTYC_BOLD: tty_code_code = 7;
pub const TTYC_BLINK: tty_code_code = 6;
pub const TTYC_BIDI: tty_code_code = 5;
pub const TTYC_BEL: tty_code_code = 4;
pub const TTYC_BCE: tty_code_code = 3;
pub const TTYC_AX: tty_code_code = 2;
pub const TTYC_AM: tty_code_code = 1;
pub const TTYC_ACSC: tty_code_code = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct clients {
    pub tqh_first: *mut client,
    pub tqh_last: *mut *mut client,
}
static mut tty_log_fd: i32 = -(1 as i32);
#[no_mangle]
pub extern "C" fn tty_create_log() {
    let mut name: [i8; 64] = [0; 64];
    (unsafe {
        xsnprintf(
            name.as_mut_ptr(),
            ::core::mem::size_of::<[i8; 64]>() as u64,
            b"tmux-out-%ld.log\0" as *const u8 as *const i8,
            getpid() as i64,
        )
    });
    (unsafe {
        tty_log_fd = open(
            name.as_mut_ptr(),
            0o1 as i32 | 0o100 as i32 | 0o1000 as i32,
            0o644 as i32,
        )
    });
    if (unsafe { tty_log_fd }) != -(1 as i32)
        && (unsafe { fcntl(tty_log_fd, 2, 1) }) == -(1 as i32)
    {
        (unsafe { fatal(b"fcntl failed\0" as *const u8 as *const i8) });
    }
}
#[no_mangle]
pub extern "C" fn tty_init(mut tty: *mut tty, mut c: *mut client) -> i32 {
    if (unsafe { isatty((*c).fd) }) == 0 {
        return -(1 as i32);
    }
    (unsafe {
        memset(
            tty as *mut (), 0,
            ::core::mem::size_of::<tty>() as u64,
        )
    });
    (unsafe { (*tty).client = c });
    (unsafe { (*tty).cstyle = SCREEN_CURSOR_DEFAULT });
    (unsafe { (*tty).ccolour = -(1 as i32) });
    (unsafe { (*tty).bg = -(1 as i32) });
    (unsafe { (*tty).fg = (*tty).bg });
    if (unsafe { tcgetattr((*c).fd, &mut (*tty).tio) }) != 0 {
        return -(1 as i32);
    }
    return 0;
}
#[no_mangle]
pub extern "C" fn tty_resize(mut tty: *mut tty) {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut ws: winsize = winsize {
        ws_row: 0,
        ws_col: 0,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };
    let mut sx: u_int = 0;
    let mut sy: u_int = 0;
    let mut xpixel: u_int = 0;
    let mut ypixel: u_int = 0;
    if (unsafe { ioctl((*c).fd, 0x5413 as i32 as u64, &mut ws as *mut winsize) }) != -(1 as i32) {
        sx = ws.ws_col as u_int;
        if sx == 0 as u32 {
            sx = 80 as u_int;
            xpixel = 0 as u_int;
        } else {
            xpixel = (ws.ws_xpixel as u32).wrapping_div(sx);
        }
        sy = ws.ws_row as u_int;
        if sy == 0 as u32 {
            sy = 24 as u_int;
            ypixel = 0 as u_int;
        } else {
            ypixel = (ws.ws_ypixel as u32).wrapping_div(sy);
        }
        if (xpixel == 0 as u32 || ypixel == 0 as u32)
            && !(unsafe { (*tty).out }).is_null()
            && (unsafe { (*tty).flags }) & 0x1000 as i32 == 0
            && (unsafe { (*(*tty).term).flags }) & 0x20 as i32 != 0
        {
            tty_puts(tty, b"\x1B[18t\x1B[14t\0" as *const u8 as *const i8);
            (unsafe { (*tty).flags |= 0x1000 as i32 });
        }
    } else {
        sx = 80 as u_int;
        sy = 24 as u_int;
        xpixel = 0 as u_int;
        ypixel = 0 as u_int;
    }
    (unsafe {
        log_debug(
            b"%s: %s now %ux%u (%ux%u)\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 11], &[i8; 11]>(b"tty_resize\0")).as_ptr(),
            (*c).name,
            sx,
            sy,
            xpixel,
            ypixel,
        )
    });
    tty_set_size(tty, sx, sy, xpixel, ypixel);
    tty_invalidate(tty);
}
#[no_mangle]
pub extern "C" fn tty_set_size(
    mut tty: *mut tty,
    mut sx: u_int,
    mut sy: u_int,
    mut xpixel: u_int,
    mut ypixel: u_int,
) {
    (unsafe { (*tty).sx = sx });
    (unsafe { (*tty).sy = sy });
    (unsafe { (*tty).xpixel = xpixel });
    (unsafe { (*tty).ypixel = ypixel });
}
extern "C" fn tty_read_callback(mut _fd: i32, mut _events: i16, mut data: *mut ()) {
    let mut tty: *mut tty = data as *mut tty;
    let mut c: *mut client = unsafe { (*tty).client };
    let mut name: *const i8 = unsafe { (*c).name };
    let mut size: size_t = unsafe { evbuffer_get_length((*tty).in_0) };
    let mut nread: i32 = 0;
    nread = unsafe { evbuffer_read((*tty).in_0, (*c).fd, -(1 as i32)) };
    if nread == 0 || nread == -(1 as i32) {
        if nread == 0 {
            (unsafe { log_debug(b"%s: read closed\0" as *const u8 as *const i8, name) });
        } else {
            (unsafe {
                log_debug(
                    b"%s: read error: %s\0" as *const u8 as *const i8,
                    name,
                    strerror(*__errno_location()),
                )
            });
        }
        (unsafe { event_del(&mut (*tty).event_in) });
        (unsafe { server_client_lost((*tty).client) });
        return;
    }
    (unsafe {
        log_debug(
            b"%s: read %d bytes (already %zu)\0" as *const u8 as *const i8,
            name,
            nread,
            size,
        )
    });
    while (unsafe { tty_keys_next(tty) }) != 0 {}
}
extern "C" fn tty_timer_callback(mut _fd: i32, mut _events: i16, mut data: *mut ()) {
    let mut tty: *mut tty = data as *mut tty;
    let mut c: *mut client = unsafe { (*tty).client };
    let mut tv: timeval = {
        let mut init = timeval {
            tv_sec: 0,
            tv_usec: 100000 as __suseconds_t,
        };
        init
    };
    (unsafe {
        log_debug(
            b"%s: %zu discarded\0" as *const u8 as *const i8,
            (*c).name,
            (*tty).discarded,
        )
    });
    (unsafe {
        (*c).flags = ((*c).flags as u64
            | ((0x8 as i32
                | 0x10 as i32
                | 0x1000000 as i32
                | 0x400 as i32
                | 0x2000000 as i32
                | KEYC_VI as i32) as u64
                | KEYC_SENT00 as u64)) as uint64_t
    });
    (unsafe {
        (*c).discarded = ((*c).discarded as u64).wrapping_add((*tty).discarded) as size_t as size_t
    });
    if (unsafe { (*tty).discarded })
        < (1 as i32 as u32).wrapping_add(
            (unsafe { (*tty).sx })
                .wrapping_mul(unsafe { (*tty).sy })
                .wrapping_div(8 as i32 as u32),
        ) as u64
    {
        (unsafe { (*tty).flags &= !(0x80 as i32) });
        tty_invalidate(tty);
        return;
    }
    (unsafe { (*tty).discarded = 0 as size_t });
    (unsafe { event_add(&mut (*tty).timer, &mut tv) });
}
extern "C" fn tty_block_maybe(mut tty: *mut tty) -> i32 {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut size: size_t = unsafe { evbuffer_get_length((*tty).out) };
    let mut tv: timeval = {
        let mut init = timeval {
            tv_sec: 0,
            tv_usec: 100000 as __suseconds_t,
        };
        init
    };
    if size == 0 as u64 {
        (unsafe { (*tty).flags &= !(0x8 as i32) });
    } else if (unsafe { (*tty).flags }) & 0x8 as i32 != 0 {
        return 0;
    }
    if size
        < (1 as i32 as u32).wrapping_add(
            (unsafe { (*tty).sx })
                .wrapping_mul(unsafe { (*tty).sy })
                .wrapping_mul(8 as i32 as u32),
        ) as u64
    {
        return 0;
    }
    if (unsafe { (*tty).flags }) & 0x80 as i32 != 0 {
        return 1;
    }
    (unsafe { (*tty).flags |= 0x80 as i32 });
    (unsafe {
        log_debug(
            b"%s: can't keep up, %zu discarded\0" as *const u8 as *const i8,
            (*c).name,
            size,
        )
    });
    (unsafe { evbuffer_drain((*tty).out, size) });
    (unsafe { (*c).discarded = ((*c).discarded as u64).wrapping_add(size) as size_t as size_t });
    (unsafe { (*tty).discarded = 0 as size_t });
    (unsafe { event_add(&mut (*tty).timer, &mut tv) });
    return 1;
}
extern "C" fn tty_write_callback(mut _fd: i32, mut _events: i16, mut data: *mut ()) {
    let mut tty: *mut tty = data as *mut tty;
    let mut c: *mut client = unsafe { (*tty).client };
    let mut size: size_t = unsafe { evbuffer_get_length((*tty).out) };
    let mut nwrite: i32 = 0;
    nwrite = unsafe { evbuffer_write((*tty).out, (*c).fd) };
    if nwrite == -(1 as i32) {
        return;
    }
    (unsafe {
        log_debug(
            b"%s: wrote %d bytes (of %zu)\0" as *const u8 as *const i8,
            (*c).name,
            nwrite,
            size,
        )
    });
    if (unsafe { (*c).redraw }) > 0 as u64 {
        if nwrite as size_t >= (unsafe { (*c).redraw }) {
            (unsafe { (*c).redraw = 0 as size_t });
        } else {
            (unsafe {
                (*c).redraw = ((*c).redraw as u64).wrapping_sub(nwrite as u64) as size_t as size_t
            });
        }
        (unsafe {
            log_debug(
                b"%s: waiting for redraw, %zu bytes left\0" as *const u8 as *const i8,
                (*c).name,
                (*c).redraw,
            )
        });
    } else if tty_block_maybe(tty) != 0 {
        return;
    }
    if (unsafe { evbuffer_get_length((*tty).out) }) != 0 as u64 {
        (unsafe { event_add(&mut (*tty).event_out, std::ptr::null::<timeval>()) });
    }
}
#[no_mangle]
pub extern "C" fn tty_open(mut tty: *mut tty, mut cause: *mut *mut i8) -> i32 {
    let mut c: *mut client = unsafe { (*tty).client };
    (unsafe {
        (*tty).term = tty_term_create(
            tty,
            (*c).term_name,
            (*c).term_caps,
            (*c).term_ncaps,
            &mut (*c).term_features,
            cause,
        )
    });
    if (unsafe { (*tty).term }).is_null() {
        tty_close(tty);
        return -(1 as i32);
    }
    (unsafe { (*tty).flags |= 0x20 as i32 });
    (unsafe { (*tty).flags &= !(0x1 as i32 | 0x2 as i32 | 0x80 as i32 | 0x4 as i32) });
    (unsafe {
        event_set(
            &mut (*tty).event_in,
            (*c).fd,
            (0x10 as i32 | 0x2 as i32) as i16,
            Some(tty_read_callback as unsafe extern "C" fn(i32, i16, *mut ()) -> ()),
            tty as *mut (),
        )
    });
    (unsafe { (*tty).in_0 = evbuffer_new() });
    if (unsafe { (*tty).in_0 }).is_null() {
        (unsafe { fatal(b"out of memory\0" as *const u8 as *const i8) });
    }
    (unsafe {
        event_set(
            &mut (*tty).event_out,
            (*c).fd,
            0x4 as i32 as i16,
            Some(tty_write_callback as unsafe extern "C" fn(i32, i16, *mut ()) -> ()),
            tty as *mut (),
        )
    });
    (unsafe { (*tty).out = evbuffer_new() });
    if (unsafe { (*tty).out }).is_null() {
        (unsafe { fatal(b"out of memory\0" as *const u8 as *const i8) });
    }
    (unsafe {
        event_set(
            &mut (*tty).timer,
            -(1 as i32), 0 as i16,
            Some(tty_timer_callback as unsafe extern "C" fn(i32, i16, *mut ()) -> ()),
            tty as *mut (),
        )
    });
    tty_start_tty(tty);
    (unsafe { tty_keys_build(tty) });
    return 0;
}
extern "C" fn tty_start_timer_callback(
    mut _fd: i32,
    mut _events: i16,
    mut data: *mut (),
) {
    let mut tty: *mut tty = data as *mut tty;
    let mut c: *mut client = unsafe { (*tty).client };
    (unsafe {
        log_debug(
            b"%s: start timer fired\0" as *const u8 as *const i8,
            (*c).name,
        )
    });
    if (unsafe { (*tty).flags }) & (0x100 as i32 | 0x800 as i32 | 0x200 as i32) == 0 {
        tty_update_features(tty);
    }
    (unsafe { (*tty).flags |= 0x100 as i32 | 0x800 as i32 | 0x200 as i32 });
}
#[no_mangle]
pub extern "C" fn tty_start_tty(mut tty: *mut tty) {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut tio: termios = termios {
        c_iflag: 0,
        c_oflag: 0,
        c_cflag: 0,
        c_lflag: 0,
        c_line: 0,
        c_cc: [0; 32],
        c_ispeed: 0,
        c_ospeed: 0,
    };
    let mut tv: timeval = {
        let mut init = timeval {
            tv_sec: 5 as __time_t,
            tv_usec: 0,
        };
        init
    };
    (unsafe { setblocking((*c).fd, 0) });
    (unsafe { event_add(&mut (*tty).event_in, std::ptr::null::<timeval>()) });
    (unsafe {
        memcpy(
            &mut tio as *mut termios as *mut (),
            &mut (*tty).tio as *mut termios as *const (),
            ::core::mem::size_of::<termios>() as u64,
        )
    });
    tio.c_iflag &= !(0o2000 as i32
        | 0o10000 as i32
        | 0o400 as i32
        | 0o100 as i32
        | 0o200 as i32
        | 0o20000 as i32
        | 0o40 as i32) as u32;
    tio.c_iflag |= 0o1 as i32 as u32;
    tio.c_oflag &= !(0o1 as i32 | 0o4 as i32 | 0o10 as i32 | 0o40 as i32) as u32;
    tio.c_lflag &= !(0o100000 as i32
        | 0o2 as i32
        | 0o10 as i32
        | 0o20 as i32
        | 0o100 as i32
        | 0o1000 as i32
        | 0o2000 as i32
        | 0o4000 as i32
        | 0o1 as i32) as u32;
    tio.c_cc[6 as i32 as usize] = 1 as cc_t;
    tio.c_cc[5 as i32 as usize] = 0 as cc_t;
    if (unsafe { tcsetattr((*c).fd, 0, &mut tio) }) == 0 {
        (unsafe { tcflush((*c).fd, 1) });
    }
    tty_putcode(tty, TTYC_SMCUP);
    tty_putcode(tty, TTYC_SMKX);
    tty_putcode(tty, TTYC_CLEAR);
    if (unsafe { tty_acs_needed(tty) }) != 0 {
        (unsafe {
            log_debug(
                b"%s: using capabilities for ACS\0" as *const u8 as *const i8,
                (*c).name,
            )
        });
        tty_putcode(tty, TTYC_ENACS);
    } else {
        (unsafe {
            log_debug(
                b"%s: using UTF-8 for ACS\0" as *const u8 as *const i8,
                (*c).name,
            )
        });
    }
    tty_putcode(tty, TTYC_CNORM);
    if (unsafe { tty_term_has((*tty).term, TTYC_KMOUS) }) != 0 {
        tty_puts(
            tty,
            b"\x1B[?1000l\x1B[?1002l\x1B[?1003l\0" as *const u8 as *const i8,
        );
        tty_puts(tty, b"\x1B[?1006l\x1B[?1005l\0" as *const u8 as *const i8);
    }
    if (unsafe { tty_term_has((*tty).term, TTYC_ENBP) }) != 0 {
        tty_putcode(tty, TTYC_ENBP);
    }
    (unsafe {
        event_set(
            &mut (*tty).start_timer,
            -(1 as i32), 0 as i16,
            Some(
                tty_start_timer_callback as unsafe extern "C" fn(i32, i16, *mut ()) -> (),
            ),
            tty as *mut (),
        )
    });
    (unsafe { event_add(&mut (*tty).start_timer, &mut tv) });
    (unsafe { (*tty).flags |= 0x10 as i32 });
    tty_invalidate(tty);
    if (unsafe { (*tty).ccolour }) != -(1 as i32) {
        tty_force_cursor_colour(tty, -(1 as i32));
    }
    (unsafe { (*tty).mouse_drag_flag = 0 });
    (unsafe { (*tty).mouse_drag_update = None });
    (unsafe { (*tty).mouse_drag_release = None });
}
#[no_mangle]
pub extern "C" fn tty_send_requests(mut tty: *mut tty) {
    if !(unsafe { (*tty).flags }) & 0x10 as i32 != 0 {
        return;
    }
    if (unsafe { (*(*tty).term).flags }) & 0x20 as i32 != 0 {
        if !(unsafe { (*(*tty).term).flags }) & 0x100 as i32 != 0 {
            tty_puts(tty, b"\x1B[c\0" as *const u8 as *const i8);
        }
        if !(unsafe { (*tty).flags }) & 0x800 as i32 != 0 {
            tty_puts(tty, b"\x1B[>c\0" as *const u8 as *const i8);
        }
        if !(unsafe { (*tty).flags }) & 0x200 as i32 != 0 {
            tty_puts(tty, b"\x1B[>q\0" as *const u8 as *const i8);
        }
        tty_puts(tty, b"\x1B]10;?\x1B\\\0" as *const u8 as *const i8);
        tty_puts(tty, b"\x1B]11;?\x1B\\\0" as *const u8 as *const i8);
    } else {
        (unsafe { (*tty).flags |= 0x100 as i32 | 0x800 as i32 | 0x200 as i32 });
    }
    (unsafe { (*tty).last_requests = time(std::ptr::null_mut::<time_t>()) });
}
#[no_mangle]
pub extern "C" fn tty_repeat_requests(mut tty: *mut tty) {
    let mut t: time_t = unsafe { time(std::ptr::null_mut::<time_t>()) };
    if !(unsafe { (*tty).flags }) & 0x10 as i32 != 0 {
        return;
    }
    if t - (unsafe { (*tty).last_requests }) <= 30 as i64 {
        return;
    }
    (unsafe { (*tty).last_requests = t });
    if (unsafe { (*(*tty).term).flags }) & 0x20 as i32 != 0 {
        tty_puts(tty, b"\x1B]10;?\x1B\\\0" as *const u8 as *const i8);
        tty_puts(tty, b"\x1B]11;?\x1B\\\0" as *const u8 as *const i8);
    }
}
#[no_mangle]
pub extern "C" fn tty_stop_tty(mut tty: *mut tty) {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut ws: winsize = winsize {
        ws_row: 0,
        ws_col: 0,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };
    if (unsafe { (*tty).flags }) & 0x10 as i32 == 0 {
        return;
    }
    (unsafe { (*tty).flags &= !(0x10 as i32) });
    (unsafe { event_del(&mut (*tty).start_timer) });
    (unsafe { event_del(&mut (*tty).timer) });
    (unsafe { (*tty).flags &= !(0x80 as i32) });
    (unsafe { event_del(&mut (*tty).event_in) });
    (unsafe { event_del(&mut (*tty).event_out) });
    if (unsafe { ioctl((*c).fd, 0x5413 as i32 as u64, &mut ws as *mut winsize) }) == -(1 as i32) {
        return;
    }
    if (unsafe { tcsetattr((*c).fd, 0, &mut (*tty).tio) }) == -(1 as i32) {
        return;
    }
    tty_raw(tty, unsafe {
        tty_term_string_ii((*tty).term, TTYC_CSR, 0, ws.ws_row as i32 - 1)
    });
    if (unsafe { tty_acs_needed(tty) }) != 0 {
        tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_RMACS) });
    }
    tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_SGR0) });
    tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_RMKX) });
    tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_CLEAR) });
    if (unsafe { (*tty).cstyle }) as u32 != SCREEN_CURSOR_DEFAULT as i32 as u32 {
        if (unsafe { tty_term_has((*tty).term, TTYC_SE) }) != 0 {
            tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_SE) });
        } else if (unsafe { tty_term_has((*tty).term, TTYC_SS) }) != 0 {
            tty_raw(tty, unsafe {
                tty_term_string_i((*tty).term, TTYC_SS, 0)
            });
        }
    }
    if (unsafe { (*tty).ccolour }) != -(1 as i32) {
        tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_CR) });
    }
    tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_CNORM) });
    if (unsafe { tty_term_has((*tty).term, TTYC_KMOUS) }) != 0 {
        tty_raw(
            tty,
            b"\x1B[?1000l\x1B[?1002l\x1B[?1003l\0" as *const u8 as *const i8,
        );
        tty_raw(tty, b"\x1B[?1006l\x1B[?1005l\0" as *const u8 as *const i8);
    }
    if (unsafe { tty_term_has((*tty).term, TTYC_DSBP) }) != 0 {
        tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_DSBP) });
    }
    if (unsafe { (*(*tty).term).flags }) & 0x20 as i32 != 0 {
        tty_raw(tty, b"\x1B[?7727l\0" as *const u8 as *const i8);
    }
    tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_DSFCS) });
    tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_DSEKS) });
    if (unsafe { (*(*tty).term).flags }) & 0x4 as i32 != 0 {
        tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_DSMG) });
    }
    tty_raw(tty, unsafe { tty_term_string((*tty).term, TTYC_RMCUP) });
    (unsafe { setblocking((*c).fd, 1) });
}
#[no_mangle]
pub extern "C" fn tty_close(mut tty: *mut tty) {
    if (unsafe { event_initialized(&mut (*tty).key_timer) }) != 0 {
        (unsafe { event_del(&mut (*tty).key_timer) });
    }
    tty_stop_tty(tty);
    if (unsafe { (*tty).flags }) & 0x20 as i32 != 0 {
        (unsafe { evbuffer_free((*tty).in_0) });
        (unsafe { event_del(&mut (*tty).event_in) });
        (unsafe { evbuffer_free((*tty).out) });
        (unsafe { event_del(&mut (*tty).event_out) });
        (unsafe { tty_term_free((*tty).term) });
        (unsafe { tty_keys_free(tty) });
        (unsafe { (*tty).flags &= !(0x20 as i32) });
    }
}
#[no_mangle]
pub extern "C" fn tty_free(mut tty: *mut tty) {
    tty_close(tty);
}
#[no_mangle]
pub extern "C" fn tty_update_features(mut tty: *mut tty) {
    let mut c: *mut client = unsafe { (*tty).client };
    if (unsafe { tty_apply_features((*tty).term, (*c).term_features) }) != 0 {
        (unsafe { tty_term_apply_overrides((*tty).term) });
    }
    if (unsafe { (*(*tty).term).flags }) & 0x4 as i32 != 0 {
        tty_putcode(tty, TTYC_ENMG);
    }
    if (unsafe { options_get_number(global_options, b"extended-keys\0" as *const u8 as *const i8) })
        != 0
    {
        tty_puts(tty, unsafe { tty_term_string((*tty).term, TTYC_ENEKS) });
    }
    if (unsafe { options_get_number(global_options, b"focus-events\0" as *const u8 as *const i8) })
        != 0
    {
        tty_puts(tty, unsafe { tty_term_string((*tty).term, TTYC_ENFCS) });
    }
    if (unsafe { (*(*tty).term).flags }) & 0x20 as i32 != 0 {
        tty_puts(tty, b"\x1B[?7727h\0" as *const u8 as *const i8);
    }
    (unsafe { server_redraw_client(c) });
    tty_invalidate(tty);
}
#[no_mangle]
pub extern "C" fn tty_raw(mut tty: *mut tty, mut s: *const i8) {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut n: ssize_t = 0;
    let mut slen: ssize_t = 0;
    let mut i: u_int = 0;
    slen = (unsafe { strlen(s) }) as ssize_t;
    i = 0 as u_int;
    while i < 5 as u32 {
        n = unsafe { write((*c).fd, s as *const (), slen as size_t) };
        if n >= 0 as i64 {
            s = unsafe { s.offset(n as isize) };
            slen -= n;
            if slen == 0 as i64 {
                break;
            }
        } else if n == -(1 as i32) as i64 && (unsafe { *__errno_location() }) != 11 {
            break;
        }
        (unsafe { usleep(100 as i32 as __useconds_t) });
        i = i.wrapping_add(1);
    }
}
#[no_mangle]
pub extern "C" fn tty_putcode(mut tty: *mut tty, mut code: tty_code_code) {
    tty_puts(tty, unsafe { tty_term_string((*tty).term, code) });
}
#[no_mangle]
pub extern "C" fn tty_putcode_i(mut tty: *mut tty, mut code: tty_code_code, mut a: i32) {
    if a < 0 {
        return;
    }
    tty_puts(tty, unsafe { tty_term_string_i((*tty).term, code, a) });
}
#[no_mangle]
pub extern "C" fn tty_putcode_ii(
    mut tty: *mut tty,
    mut code: tty_code_code,
    mut a: i32,
    mut b: i32,
) {
    if a < 0 || b < 0 {
        return;
    }
    tty_puts(tty, unsafe { tty_term_string_ii((*tty).term, code, a, b) });
}
#[no_mangle]
pub extern "C" fn tty_putcode_iii(
    mut tty: *mut tty,
    mut code: tty_code_code,
    mut a: i32,
    mut b: i32,
    mut c: i32,
) {
    if a < 0 || b < 0 || c < 0 {
        return;
    }
    tty_puts(tty, unsafe {
        tty_term_string_iii((*tty).term, code, a, b, c)
    });
}
#[no_mangle]
pub extern "C" fn tty_putcode_s(mut tty: *mut tty, mut code: tty_code_code, mut a: *const i8) {
    if !a.is_null() {
        tty_puts(tty, unsafe { tty_term_string_s((*tty).term, code, a) });
    }
}
#[no_mangle]
pub extern "C" fn tty_putcode_ss(
    mut tty: *mut tty,
    mut code: tty_code_code,
    mut a: *const i8,
    mut b: *const i8,
) {
    if !a.is_null() && !b.is_null() {
        tty_puts(tty, unsafe { tty_term_string_ss((*tty).term, code, a, b) });
    }
}
extern "C" fn tty_add(mut tty: *mut tty, mut buf: *const i8, mut len: size_t) {
    let mut c: *mut client = unsafe { (*tty).client };
    if (unsafe { (*tty).flags }) & 0x80 as i32 != 0 {
        (unsafe {
            (*tty).discarded = ((*tty).discarded as u64).wrapping_add(len) as size_t as size_t
        });
        return;
    }
    (unsafe { evbuffer_add((*tty).out, buf as *const (), len) });
    (unsafe {
        log_debug(
            b"%s: %.*s\0" as *const u8 as *const i8,
            (*c).name,
            len as i32,
            buf,
        )
    });
    (unsafe { (*c).written = ((*c).written as u64).wrapping_add(len) as size_t as size_t });
    if (unsafe { tty_log_fd }) != -(1 as i32) {
        (unsafe { write(tty_log_fd, buf as *const (), len) });
    }
    if (unsafe { (*tty).flags }) & 0x10 as i32 != 0 {
        (unsafe { event_add(&mut (*tty).event_out, std::ptr::null::<timeval>()) });
    }
}
#[no_mangle]
pub extern "C" fn tty_puts(mut tty: *mut tty, mut s: *const i8) {
    if (unsafe { *s }) as i32 != '\0' as i32 {
        tty_add(tty, s, unsafe { strlen(s) });
    }
}
#[no_mangle]
pub extern "C" fn tty_putc(mut tty: *mut tty, mut ch: u_char) {
    let mut acs: *const i8 = std::ptr::null::<i8>();
    if (unsafe { (*(*tty).term).flags }) & 0x2 as i32 != 0
        && ch as i32 >= 0x20 as i32
        && ch as i32 != 0x7f as i32
        && (unsafe { (*tty).cy }) == (unsafe { (*tty).sy }).wrapping_sub(1 as i32 as u32)
        && (unsafe { (*tty).cx }).wrapping_add(1 as i32 as u32) >= (unsafe { (*tty).sx })
    {
        return;
    }
    if (unsafe { (*tty).cell.attr }) as i32 & 0x80 as i32 != 0 {
        acs = unsafe { tty_acs_get(tty, ch) };
        if !acs.is_null() {
            tty_add(tty, acs, unsafe { strlen(acs) });
        } else {
            tty_add(tty, &mut ch as *mut u_char as *const i8, 1 as size_t);
        }
    } else {
        tty_add(tty, &mut ch as *mut u_char as *const i8, 1 as size_t);
    }
    if ch as i32 >= 0x20 as i32 && ch as i32 != 0x7f as i32 {
        if (unsafe { (*tty).cx }) >= (unsafe { (*tty).sx }) {
            (unsafe { (*tty).cx = 1 as u_int });
            if (unsafe { (*tty).cy }) != (unsafe { (*tty).rlower }) {
                (unsafe { (*tty).cy = ((*tty).cy).wrapping_add(1) });
            }
            if (unsafe { (*(*tty).term).flags }) & 0x2 as i32 != 0 {
                tty_putcode_ii(
                    tty,
                    TTYC_CUP,
                    (unsafe { (*tty).cy }) as i32,
                    (unsafe { (*tty).cx }) as i32,
                );
            }
        } else {
            (unsafe { (*tty).cx = ((*tty).cx).wrapping_add(1) });
        }
    }
}
#[no_mangle]
pub extern "C" fn tty_putn(
    mut tty: *mut tty,
    mut buf: *const (),
    mut len: size_t,
    mut width: u_int,
) {
    if (unsafe { (*(*tty).term).flags }) & 0x2 as i32 != 0
        && (unsafe { (*tty).cy }) == (unsafe { (*tty).sy }).wrapping_sub(1 as i32 as u32)
        && ((unsafe { (*tty).cx }) as u64).wrapping_add(len) >= (unsafe { (*tty).sx }) as u64
    {
        len = (unsafe { (*tty).sx })
            .wrapping_sub(unsafe { (*tty).cx })
            .wrapping_sub(1 as i32 as u32) as size_t;
    }
    tty_add(tty, buf as *const i8, len);
    if (unsafe { (*tty).cx }).wrapping_add(width) > (unsafe { (*tty).sx }) {
        (unsafe { (*tty).cx = ((*tty).cx).wrapping_add(width).wrapping_sub((*tty).sx) });
        if (unsafe { (*tty).cx }) <= (unsafe { (*tty).sx }) {
            (unsafe { (*tty).cy = ((*tty).cy).wrapping_add(1) });
        } else {
            (unsafe {
                (*tty).cy = (2147483647 as i32 as u32)
                    .wrapping_mul(2 as u32)
                    .wrapping_add(1 as u32)
            });
            (unsafe { (*tty).cx = (*tty).cy });
        }
    } else {
        (unsafe { (*tty).cx = ((*tty).cx as u32).wrapping_add(width) as u_int as u_int });
    };
}
extern "C" fn tty_set_italics(mut tty: *mut tty) {
    let mut s: *const i8 = std::ptr::null::<i8>();
    if (unsafe { tty_term_has((*tty).term, TTYC_SITM) }) != 0 {
        s = unsafe {
            options_get_string(
                global_options,
                b"default-terminal\0" as *const u8 as *const i8,
            )
        };
        if (unsafe { strcmp(s, b"screen\0" as *const u8 as *const i8) }) != 0
            && (unsafe { strncmp(s, b"screen-\0" as *const u8 as *const i8, 7 as u64) })
                != 0
        {
            tty_putcode(tty, TTYC_SITM);
            return;
        }
    }
    tty_putcode(tty, TTYC_SMSO);
}
#[no_mangle]
pub extern "C" fn tty_set_title(mut tty: *mut tty, mut title: *const i8) {
    if (unsafe { tty_term_has((*tty).term, TTYC_TSL) }) == 0
        || (unsafe { tty_term_has((*tty).term, TTYC_FSL) }) == 0
    {
        return;
    }
    tty_putcode(tty, TTYC_TSL);
    tty_puts(tty, title);
    tty_putcode(tty, TTYC_FSL);
}
#[no_mangle]
pub extern "C" fn tty_set_path(mut tty: *mut tty, mut title: *const i8) {
    if (unsafe { tty_term_has((*tty).term, TTYC_SWD) }) == 0
        || (unsafe { tty_term_has((*tty).term, TTYC_FSL) }) == 0
    {
        return;
    }
    tty_putcode(tty, TTYC_SWD);
    tty_puts(tty, title);
    tty_putcode(tty, TTYC_FSL);
}
extern "C" fn tty_force_cursor_colour(mut tty: *mut tty, mut c: i32) {
    let mut r: u_char = 0;
    let mut g: u_char = 0;
    let mut b: u_char = 0;
    let mut s: [i8; 13] = [0; 13];
    if c != -(1 as i32) {
        c = unsafe { colour_force_rgb(c) };
    }
    if c == (unsafe { (*tty).ccolour }) {
        return;
    }
    if c == -(1 as i32) {
        tty_putcode(tty, TTYC_CR);
    } else {
        (unsafe { colour_split_rgb(c, &mut r, &mut g, &mut b) });
        (unsafe {
            xsnprintf(
                s.as_mut_ptr(),
                ::core::mem::size_of::<[i8; 13]>() as u64,
                b"rgb:%02hhx/%02hhx/%02hhx\0" as *const u8 as *const i8,
                r as i32,
                g as i32,
                b as i32,
            )
        });
        tty_putcode_s(tty, TTYC_CS, s.as_mut_ptr());
    }
    (unsafe { (*tty).ccolour = c });
}
extern "C" fn tty_update_cursor(mut tty: *mut tty, mut mode: i32, mut s: *mut screen) -> i32 {
    let mut cstyle: screen_cursor_style = SCREEN_CURSOR_DEFAULT;
    let mut ccolour: i32 = 0;
    let mut changed: i32 = 0;
    let mut cmode: i32 = mode;
    if !s.is_null() {
        ccolour = unsafe { (*s).ccolour };
        if (unsafe { (*s).ccolour }) == -(1 as i32) {
            ccolour = unsafe { (*s).default_ccolour };
        }
        tty_force_cursor_colour(tty, ccolour);
    }
    if !cmode & 0x1 as i32 != 0 {
        if (unsafe { (*tty).mode }) & 0x1 as i32 != 0 {
            tty_putcode(tty, TTYC_CIVIS);
        }
        return cmode;
    }
    if s.is_null() {
        cstyle = unsafe { (*tty).cstyle };
    } else {
        cstyle = unsafe { (*s).cstyle };
        if cstyle as u32 == SCREEN_CURSOR_DEFAULT as i32 as u32 {
            if !cmode & 0x20000 as i32 != 0 {
                if (unsafe { (*s).default_mode }) & 0x80 as i32 != 0 {
                    cmode |= 0x80 as i32;
                } else {
                    cmode &= !(0x80 as i32);
                }
            }
            cstyle = unsafe { (*s).default_cstyle };
        }
    }
    changed = cmode ^ (unsafe { (*tty).mode });
    if changed & (0x1 as i32 | 0x80 as i32 | 0x10000 as i32) == 0
        && cstyle as u32 == (unsafe { (*tty).cstyle }) as u32
    {
        return cmode;
    }
    tty_putcode(tty, TTYC_CNORM);
    match cstyle as u32 {
        0 => {
            if (unsafe { (*tty).cstyle }) as u32 != SCREEN_CURSOR_DEFAULT as i32 as u32 {
                if (unsafe { tty_term_has((*tty).term, TTYC_SE) }) != 0 {
                    tty_putcode(tty, TTYC_SE);
                } else {
                    tty_putcode_i(tty, TTYC_SS, 0);
                }
            }
            if cmode & (0x80 as i32 | 0x10000 as i32) != 0 {
                tty_putcode(tty, TTYC_CVVIS);
            }
        }
        1 => {
            if (unsafe { tty_term_has((*tty).term, TTYC_SS) }) != 0 {
                if cmode & 0x80 as i32 != 0 {
                    tty_putcode_i(tty, TTYC_SS, 1);
                } else {
                    tty_putcode_i(tty, TTYC_SS, 2);
                }
            } else if cmode & 0x80 as i32 != 0 {
                tty_putcode(tty, TTYC_CVVIS);
            }
        }
        2 => {
            if (unsafe { tty_term_has((*tty).term, TTYC_SS) }) != 0 {
                if cmode & 0x80 as i32 != 0 {
                    tty_putcode_i(tty, TTYC_SS, 3);
                } else {
                    tty_putcode_i(tty, TTYC_SS, 4);
                }
            } else if cmode & 0x80 as i32 != 0 {
                tty_putcode(tty, TTYC_CVVIS);
            }
        }
        3 => {
            if (unsafe { tty_term_has((*tty).term, TTYC_SS) }) != 0 {
                if cmode & 0x80 as i32 != 0 {
                    tty_putcode_i(tty, TTYC_SS, 5);
                } else {
                    tty_putcode_i(tty, TTYC_SS, 6);
                }
            } else if cmode & 0x80 as i32 != 0 {
                tty_putcode(tty, TTYC_CVVIS);
            }
        }
        _ => {}
    }
    (unsafe { (*tty).cstyle = cstyle });
    return cmode;
}
#[no_mangle]
pub extern "C" fn tty_update_mode(mut tty: *mut tty, mut mode: i32, mut s: *mut screen) {
    let mut term: *mut tty_term = unsafe { (*tty).term };
    let mut c: *mut client = unsafe { (*tty).client };
    let mut changed: i32 = 0;
    if (unsafe { (*tty).flags }) & 0x1 as i32 != 0 {
        mode &= !(0x1 as i32);
    }
    if tty_update_cursor(tty, mode, s) & 0x80 as i32 != 0 {
        mode |= 0x80 as i32;
    } else {
        mode &= !(0x80 as i32);
    }
    changed = mode ^ (unsafe { (*tty).mode });
    if (unsafe { log_get_level() }) != 0 && changed != 0 {
        (unsafe {
            log_debug(
                b"%s: current mode %s\0" as *const u8 as *const i8,
                (*c).name,
                screen_mode_to_string((*tty).mode),
            )
        });
        (unsafe {
            log_debug(
                b"%s: setting mode %s\0" as *const u8 as *const i8,
                (*c).name,
                screen_mode_to_string(mode),
            )
        });
    }
    if changed & (0x20 as i32 | 0x40 as i32 | 0x1000 as i32) != 0
        && (unsafe { tty_term_has(term, TTYC_KMOUS) }) != 0
    {
        tty_puts(
            tty,
            b"\x1B[?1006l\x1B[?1000l\x1B[?1002l\x1B[?1003l\0" as *const u8 as *const i8,
        );
        if mode & (0x20 as i32 | 0x40 as i32 | 0x1000 as i32) != 0 {
            tty_puts(tty, b"\x1B[?1006h\0" as *const u8 as *const i8);
        }
        if mode & 0x1000 as i32 != 0 {
            tty_puts(
                tty,
                b"\x1B[?1000h\x1B[?1002h\x1B[?1003h\0" as *const u8 as *const i8,
            );
        } else if mode & 0x40 as i32 != 0 {
            tty_puts(tty, b"\x1B[?1000h\x1B[?1002h\0" as *const u8 as *const i8);
        } else if mode & 0x20 as i32 != 0 {
            tty_puts(tty, b"\x1B[?1000h\0" as *const u8 as *const i8);
        }
    }
    (unsafe { (*tty).mode = mode });
}
extern "C" fn tty_emulate_repeat(
    mut tty: *mut tty,
    mut code: tty_code_code,
    mut code1: tty_code_code,
    mut n: u_int,
) {
    if (unsafe { tty_term_has((*tty).term, code) }) != 0 {
        tty_putcode_i(tty, code, n as i32);
    } else {
        loop {
            let fresh0 = n;
            n = n.wrapping_sub(1);
            if fresh0 <= 0 as u32 {
                break;
            }
            tty_putcode(tty, code1);
        }
    };
}
extern "C" fn tty_repeat_space(mut tty: *mut tty, mut n: u_int) {
    static mut s: [i8; 500] = [0; 500];
    if (unsafe { *s.as_mut_ptr() }) as i32 != ' ' as i32 {
        (unsafe {
            memset(
                s.as_mut_ptr() as *mut (),
                ' ' as i32,
                ::core::mem::size_of::<[i8; 500]>() as u64,
            )
        });
    }
    while n as u64 > ::core::mem::size_of::<[i8; 500]>() as u64 {
        tty_putn(
            tty,
            (unsafe { s.as_mut_ptr() }) as *const (),
            ::core::mem::size_of::<[i8; 500]>() as u64,
            ::core::mem::size_of::<[i8; 500]>() as u64 as u_int,
        );
        n = (n as u64).wrapping_sub(::core::mem::size_of::<[i8; 500]>() as u64) as u_int as u_int;
    }
    if n != 0 as u32 {
        tty_putn(
            tty,
            (unsafe { s.as_mut_ptr() }) as *const (),
            n as size_t,
            n,
        );
    }
}
#[no_mangle]
pub extern "C" fn tty_window_bigger(mut tty: *mut tty) -> i32 {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut w: *mut window = unsafe { (*(*(*c).session).curw).window };
    return ((unsafe { (*tty).sx }) < (unsafe { (*w).sx })
        || (unsafe { (*tty).sy }).wrapping_sub(unsafe { status_line_size(c) })
            < (unsafe { (*w).sy })) as i32;
}
#[no_mangle]
pub extern "C" fn tty_window_offset(
    mut tty: *mut tty,
    mut ox: *mut u_int,
    mut oy: *mut u_int,
    mut sx: *mut u_int,
    mut sy: *mut u_int,
) -> i32 {
    (unsafe { *ox = (*tty).oox });
    (unsafe { *oy = (*tty).ooy });
    (unsafe { *sx = (*tty).osx });
    (unsafe { *sy = (*tty).osy });
    return unsafe { (*tty).oflag };
}
extern "C" fn tty_window_offset1(
    mut tty: *mut tty,
    mut ox: *mut u_int,
    mut oy: *mut u_int,
    mut sx: *mut u_int,
    mut sy: *mut u_int,
) -> i32 {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut w: *mut window = unsafe { (*(*(*c).session).curw).window };
    let mut wp: *mut window_pane = unsafe { server_client_get_pane(c) };
    let mut cx: u_int = 0;
    let mut cy: u_int = 0;
    let mut lines: u_int = 0;
    lines = unsafe { status_line_size(c) };
    if (unsafe { (*tty).sx }) >= (unsafe { (*w).sx })
        && (unsafe { (*tty).sy }).wrapping_sub(lines) >= (unsafe { (*w).sy })
    {
        (unsafe { *ox = 0 as u_int });
        (unsafe { *oy = 0 as u_int });
        (unsafe { *sx = (*w).sx });
        (unsafe { *sy = (*w).sy });
        (unsafe { (*c).pan_window = std::ptr::null_mut::<()>() });
        return 0;
    }
    (unsafe { *sx = (*tty).sx });
    (unsafe { *sy = ((*tty).sy).wrapping_sub(lines) });
    if (unsafe { (*c).pan_window }) == w as *mut () {
        if (unsafe { *sx }) >= (unsafe { (*w).sx }) {
            (unsafe { (*c).pan_ox = 0 as u_int });
        } else if (unsafe { (*c).pan_ox }).wrapping_add(unsafe { *sx }) > (unsafe { (*w).sx }) {
            (unsafe { (*c).pan_ox = ((*w).sx).wrapping_sub(*sx) });
        }
        (unsafe { *ox = (*c).pan_ox });
        if (unsafe { *sy }) >= (unsafe { (*w).sy }) {
            (unsafe { (*c).pan_oy = 0 as u_int });
        } else if (unsafe { (*c).pan_oy }).wrapping_add(unsafe { *sy }) > (unsafe { (*w).sy }) {
            (unsafe { (*c).pan_oy = ((*w).sy).wrapping_sub(*sy) });
        }
        (unsafe { *oy = (*c).pan_oy });
        return 1;
    }
    if !(unsafe { (*(*wp).screen).mode }) & 0x1 as i32 != 0 {
        (unsafe { *ox = 0 as u_int });
        (unsafe { *oy = 0 as u_int });
    } else {
        cx = (unsafe { (*wp).xoff }).wrapping_add(unsafe { (*(*wp).screen).cx });
        cy = (unsafe { (*wp).yoff }).wrapping_add(unsafe { (*(*wp).screen).cy });
        if cx < (unsafe { *sx }) {
            (unsafe { *ox = 0 as u_int });
        } else if cx > (unsafe { (*w).sx }).wrapping_sub(unsafe { *sx }) {
            (unsafe { *ox = ((*w).sx).wrapping_sub(*sx) });
        } else {
            (unsafe { *ox = cx.wrapping_sub((*sx).wrapping_div(2 as i32 as u32)) });
        }
        if cy < (unsafe { *sy }) {
            (unsafe { *oy = 0 as u_int });
        } else if cy > (unsafe { (*w).sy }).wrapping_sub(unsafe { *sy }) {
            (unsafe { *oy = ((*w).sy).wrapping_sub(*sy) });
        } else {
            (unsafe { *oy = cy.wrapping_sub((*sy).wrapping_div(2 as i32 as u32)) });
        }
    }
    (unsafe { (*c).pan_window = std::ptr::null_mut::<()>() });
    return 1;
}
#[no_mangle]
pub extern "C" fn tty_update_window_offset(mut w: *mut window) {
    let mut c: *mut client = std::ptr::null_mut::<client>();
    c = unsafe { clients.tqh_first };
    while !c.is_null() {
        if !(unsafe { (*c).session }).is_null()
            && !(unsafe { (*(*c).session).curw }).is_null()
            && (unsafe { (*(*(*c).session).curw).window }) == w
        {
            tty_update_client_offset(c);
        }
        c = unsafe { (*c).entry.tqe_next };
    }
}
#[no_mangle]
pub extern "C" fn tty_update_client_offset(mut c: *mut client) {
    let mut ox: u_int = 0;
    let mut oy: u_int = 0;
    let mut sx: u_int = 0;
    let mut sy: u_int = 0;
    if !(unsafe { (*c).flags }) & 0x1 as i32 as u64 != 0 {
        return;
    }
    (unsafe {
        (*c).tty.oflag = tty_window_offset1(&mut (*c).tty, &mut ox, &mut oy, &mut sx, &mut sy)
    });
    if ox == (unsafe { (*c).tty.oox })
        && oy == (unsafe { (*c).tty.ooy })
        && sx == (unsafe { (*c).tty.osx })
        && sy == (unsafe { (*c).tty.osy })
    {
        return;
    }
    (unsafe {
        log_debug(
            b"%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 25], &[i8; 25]>(b"tty_update_client_offset\0"))
                .as_ptr(),
            (*c).name,
            (*c).tty.oox,
            (*c).tty.ooy,
            (*c).tty.osx,
            (*c).tty.osy,
            ox,
            oy,
            sx,
            sy,
        )
    });
    (unsafe { (*c).tty.oox = ox });
    (unsafe { (*c).tty.ooy = oy });
    (unsafe { (*c).tty.osx = sx });
    (unsafe { (*c).tty.osy = sy });
    (unsafe { (*c).flags |= (0x8 as i32 | 0x10 as i32) as u64 });
}
extern "C" fn tty_large_region(mut _tty: *mut tty, mut ctx: *const tty_ctx) -> i32 {
    return ((unsafe { (*ctx).orlower }).wrapping_sub(unsafe { (*ctx).orupper })
        >= (unsafe { (*ctx).sy }).wrapping_div(2 as i32 as u32)) as i32;
}
extern "C" fn tty_fake_bce(mut tty: *const tty, mut gc: *const grid_cell, mut bg: u_int) -> i32 {
    if (unsafe { tty_term_flag((*tty).term, TTYC_BCE) }) != 0 {
        return 0;
    }
    if !(bg == 8 as u32 || bg == 9 as u32)
        || !((unsafe { (*gc).bg }) == 8 || (unsafe { (*gc).bg }) == 9)
    {
        return 1;
    }
    return 0;
}
extern "C" fn tty_redraw_region(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut i: u_int = 0;
    if tty_large_region(tty, ctx) != 0 {
        (unsafe {
            log_debug(
                b"%s: %s large redraw\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 18], &[i8; 18]>(b"tty_redraw_region\0")).as_ptr(),
                (*c).name,
            )
        });
        (unsafe { ((*ctx).redraw_cb).expect("non-null function pointer")(ctx) });
        return;
    }
    i = unsafe { (*ctx).orupper };
    while i <= (unsafe { (*ctx).orlower }) {
        tty_draw_pane(tty, ctx, i);
        i = i.wrapping_add(1);
    }
}
extern "C" fn tty_is_visible(
    mut _tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut ny: u_int,
) -> i32 {
    let mut xoff: u_int = (unsafe { (*ctx).rxoff }).wrapping_add(px);
    let mut yoff: u_int = (unsafe { (*ctx).ryoff }).wrapping_add(py);
    if (unsafe { (*ctx).bigger }) == 0 {
        return 1;
    }
    if xoff.wrapping_add(nx) <= (unsafe { (*ctx).wox })
        || xoff >= (unsafe { (*ctx).wox }).wrapping_add(unsafe { (*ctx).wsx })
        || yoff.wrapping_add(ny) <= (unsafe { (*ctx).woy })
        || yoff >= (unsafe { (*ctx).woy }).wrapping_add(unsafe { (*ctx).wsy })
    {
        return 0;
    }
    return 1;
}
extern "C" fn tty_clamp_line(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut i: *mut u_int,
    mut x: *mut u_int,
    mut rx: *mut u_int,
    mut ry: *mut u_int,
) -> i32 {
    let mut xoff: u_int = (unsafe { (*ctx).rxoff }).wrapping_add(px);
    if tty_is_visible(tty, ctx, px, py, nx, 1 as u_int) == 0 {
        return 0;
    }
    (unsafe { *ry = ((*ctx).yoff).wrapping_add(py).wrapping_sub((*ctx).woy) });
    if xoff >= (unsafe { (*ctx).wox })
        && xoff.wrapping_add(nx) <= (unsafe { (*ctx).wox }).wrapping_add(unsafe { (*ctx).wsx })
    {
        (unsafe { *i = 0 as u_int });
        (unsafe { *x = ((*ctx).xoff).wrapping_add(px).wrapping_sub((*ctx).wox) });
        (unsafe { *rx = nx });
    } else if xoff < (unsafe { (*ctx).wox })
        && xoff.wrapping_add(nx) > (unsafe { (*ctx).wox }).wrapping_add(unsafe { (*ctx).wsx })
    {
        (unsafe { *i = (*ctx).wox });
        (unsafe { *x = 0 as u_int });
        (unsafe { *rx = (*ctx).wsx });
    } else if xoff < (unsafe { (*ctx).wox }) {
        (unsafe { *i = ((*ctx).wox).wrapping_sub(((*ctx).xoff).wrapping_add(px)) });
        (unsafe { *x = 0 as u_int });
        (unsafe { *rx = nx.wrapping_sub(*i) });
    } else {
        (unsafe { *i = 0 as u_int });
        (unsafe { *x = ((*ctx).xoff).wrapping_add(px).wrapping_sub((*ctx).wox) });
        (unsafe { *rx = ((*ctx).wsx).wrapping_sub(*x) });
    }
    if (unsafe { *rx }) > nx {
        (unsafe {
            fatalx(
                b"%s: x too big, %u > %u\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 15], &[i8; 15]>(b"tty_clamp_line\0")).as_ptr(),
                *rx,
                nx,
            )
        });
    }
    return 1;
}
extern "C" fn tty_clear_line(
    mut tty: *mut tty,
    mut defaults: *const grid_cell,
    mut py: u_int,
    mut px: u_int,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut r: overlay_ranges = overlay_ranges {
        px: [0; 3],
        nx: [0; 3],
    };
    let mut i: u_int = 0;
    (unsafe {
        log_debug(
            b"%s: %s, %u at %u,%u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 15], &[i8; 15]>(b"tty_clear_line\0")).as_ptr(),
            (*c).name,
            nx,
            px,
            py,
        )
    });
    if nx == 0 as u32 {
        return;
    }
    if (unsafe { ((*c).overlay_check).is_none() }) && tty_fake_bce(tty, defaults, bg) == 0 {
        if px.wrapping_add(nx) >= (unsafe { (*tty).sx })
            && (unsafe { tty_term_has((*tty).term, TTYC_EL) }) != 0
        {
            tty_cursor(tty, px, py);
            tty_putcode(tty, TTYC_EL);
            return;
        }
        if px == 0 as u32 && (unsafe { tty_term_has((*tty).term, TTYC_EL1) }) != 0 {
            tty_cursor(tty, px.wrapping_add(nx).wrapping_sub(1 as i32 as u32), py);
            tty_putcode(tty, TTYC_EL1);
            return;
        }
        if (unsafe { tty_term_has((*tty).term, TTYC_ECH) }) != 0 {
            tty_cursor(tty, px, py);
            tty_putcode_i(tty, TTYC_ECH, nx as i32);
            return;
        }
    }
    tty_check_overlay_range(tty, px, py, nx, &mut r);
    i = 0 as u_int;
    while i < 3 as u32 {
        if r.nx[i as usize] != 0 as u32 {
            tty_cursor(tty, r.px[i as usize], py);
            tty_repeat_space(tty, r.nx[i as usize]);
        }
        i = i.wrapping_add(1);
    }
}
extern "C" fn tty_clear_pane_line(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut py: u_int,
    mut px: u_int,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut i: u_int = 0;
    let mut x: u_int = 0;
    let mut rx: u_int = 0;
    let mut ry: u_int = 0;
    (unsafe {
        log_debug(
            b"%s: %s, %u at %u,%u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 20], &[i8; 20]>(b"tty_clear_pane_line\0")).as_ptr(),
            (*c).name,
            nx,
            px,
            py,
        )
    });
    if tty_clamp_line(tty, ctx, px, py, nx, &mut i, &mut x, &mut rx, &mut ry) != 0 {
        tty_clear_line(tty, unsafe { &(*ctx).defaults }, ry, x, rx, bg);
    }
}
extern "C" fn tty_clamp_area(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut ny: u_int,
    mut i: *mut u_int,
    mut j: *mut u_int,
    mut x: *mut u_int,
    mut y: *mut u_int,
    mut rx: *mut u_int,
    mut ry: *mut u_int,
) -> i32 {
    let mut xoff: u_int = (unsafe { (*ctx).rxoff }).wrapping_add(px);
    let mut yoff: u_int = (unsafe { (*ctx).ryoff }).wrapping_add(py);
    if tty_is_visible(tty, ctx, px, py, nx, ny) == 0 {
        return 0;
    }
    if xoff >= (unsafe { (*ctx).wox })
        && xoff.wrapping_add(nx) <= (unsafe { (*ctx).wox }).wrapping_add(unsafe { (*ctx).wsx })
    {
        (unsafe { *i = 0 as u_int });
        (unsafe { *x = ((*ctx).xoff).wrapping_add(px).wrapping_sub((*ctx).wox) });
        (unsafe { *rx = nx });
    } else if xoff < (unsafe { (*ctx).wox })
        && xoff.wrapping_add(nx) > (unsafe { (*ctx).wox }).wrapping_add(unsafe { (*ctx).wsx })
    {
        (unsafe { *i = (*ctx).wox });
        (unsafe { *x = 0 as u_int });
        (unsafe { *rx = (*ctx).wsx });
    } else if xoff < (unsafe { (*ctx).wox }) {
        (unsafe { *i = ((*ctx).wox).wrapping_sub(((*ctx).xoff).wrapping_add(px)) });
        (unsafe { *x = 0 as u_int });
        (unsafe { *rx = nx.wrapping_sub(*i) });
    } else {
        (unsafe { *i = 0 as u_int });
        (unsafe { *x = ((*ctx).xoff).wrapping_add(px).wrapping_sub((*ctx).wox) });
        (unsafe { *rx = ((*ctx).wsx).wrapping_sub(*x) });
    }
    if (unsafe { *rx }) > nx {
        (unsafe {
            fatalx(
                b"%s: x too big, %u > %u\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 15], &[i8; 15]>(b"tty_clamp_area\0")).as_ptr(),
                *rx,
                nx,
            )
        });
    }
    if yoff >= (unsafe { (*ctx).woy })
        && yoff.wrapping_add(ny) <= (unsafe { (*ctx).woy }).wrapping_add(unsafe { (*ctx).wsy })
    {
        (unsafe { *j = 0 as u_int });
        (unsafe { *y = ((*ctx).yoff).wrapping_add(py).wrapping_sub((*ctx).woy) });
        (unsafe { *ry = ny });
    } else if yoff < (unsafe { (*ctx).woy })
        && yoff.wrapping_add(ny) > (unsafe { (*ctx).woy }).wrapping_add(unsafe { (*ctx).wsy })
    {
        (unsafe { *j = (*ctx).woy });
        (unsafe { *y = 0 as u_int });
        (unsafe { *ry = (*ctx).wsy });
    } else if yoff < (unsafe { (*ctx).woy }) {
        (unsafe { *j = ((*ctx).woy).wrapping_sub(((*ctx).yoff).wrapping_add(py)) });
        (unsafe { *y = 0 as u_int });
        (unsafe { *ry = ny.wrapping_sub(*j) });
    } else {
        (unsafe { *j = 0 as u_int });
        (unsafe { *y = ((*ctx).yoff).wrapping_add(py).wrapping_sub((*ctx).woy) });
        (unsafe { *ry = ((*ctx).wsy).wrapping_sub(*y) });
    }
    if (unsafe { *ry }) > ny {
        (unsafe {
            fatalx(
                b"%s: y too big, %u > %u\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 15], &[i8; 15]>(b"tty_clamp_area\0")).as_ptr(),
                *ry,
                ny,
            )
        });
    }
    return 1;
}
extern "C" fn tty_clear_area(
    mut tty: *mut tty,
    mut defaults: *const grid_cell,
    mut py: u_int,
    mut ny: u_int,
    mut px: u_int,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut yy: u_int = 0;
    let mut tmp: [i8; 64] = [0; 64];
    (unsafe {
        log_debug(
            b"%s: %s, %u,%u at %u,%u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 15], &[i8; 15]>(b"tty_clear_area\0")).as_ptr(),
            (*c).name,
            nx,
            ny,
            px,
            py,
        )
    });
    if nx == 0 as u32 || ny == 0 as u32 {
        return;
    }
    if (unsafe { ((*c).overlay_check).is_none() }) && tty_fake_bce(tty, defaults, bg) == 0 {
        if px == 0 as u32
            && px.wrapping_add(nx) >= (unsafe { (*tty).sx })
            && py.wrapping_add(ny) >= (unsafe { (*tty).sy })
            && (unsafe { tty_term_has((*tty).term, TTYC_ED) }) != 0
        {
            tty_cursor(tty, 0 as u_int, py);
            tty_putcode(tty, TTYC_ED);
            return;
        }
        if (unsafe { (*(*tty).term).flags }) & 0x8 as i32 != 0
            && !(bg == 8 as u32 || bg == 9 as u32)
        {
            (unsafe {
                xsnprintf(
                    tmp.as_mut_ptr(),
                    ::core::mem::size_of::<[i8; 64]>() as u64,
                    b"\x1B[32;%u;%u;%u;%u$x\0" as *const u8 as *const i8,
                    py.wrapping_add(1 as i32 as u32),
                    px.wrapping_add(1 as i32 as u32),
                    py.wrapping_add(ny),
                    px.wrapping_add(nx),
                )
            });
            tty_puts(tty, tmp.as_mut_ptr());
            return;
        }
        if px == 0 as u32
            && px.wrapping_add(nx) >= (unsafe { (*tty).sx })
            && ny > 2 as u32
            && (unsafe { tty_term_has((*tty).term, TTYC_CSR) }) != 0
            && (unsafe { tty_term_has((*tty).term, TTYC_INDN) }) != 0
        {
            tty_region(tty, py, py.wrapping_add(ny).wrapping_sub(1 as i32 as u32));
            tty_margin_off(tty);
            tty_putcode_i(tty, TTYC_INDN, ny as i32);
            return;
        }
        if nx > 2 as u32
            && ny > 2 as u32
            && (unsafe { tty_term_has((*tty).term, TTYC_CSR) }) != 0
            && (unsafe { (*(*tty).term).flags }) & 0x4 as i32 != 0
            && (unsafe { tty_term_has((*tty).term, TTYC_INDN) }) != 0
        {
            tty_region(tty, py, py.wrapping_add(ny).wrapping_sub(1 as i32 as u32));
            tty_margin(tty, px, px.wrapping_add(nx).wrapping_sub(1 as i32 as u32));
            tty_putcode_i(tty, TTYC_INDN, ny as i32);
            return;
        }
    }
    yy = py;
    while yy < py.wrapping_add(ny) {
        tty_clear_line(tty, defaults, yy, px, nx, bg);
        yy = yy.wrapping_add(1);
    }
}
extern "C" fn tty_clear_pane_area(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut py: u_int,
    mut ny: u_int,
    mut px: u_int,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut i: u_int = 0;
    let mut j: u_int = 0;
    let mut x: u_int = 0;
    let mut y: u_int = 0;
    let mut rx: u_int = 0;
    let mut ry: u_int = 0;
    if tty_clamp_area(
        tty, ctx, px, py, nx, ny, &mut i, &mut j, &mut x, &mut y, &mut rx, &mut ry,
    ) != 0
    {
        tty_clear_area(tty, unsafe { &(*ctx).defaults }, y, ry, x, rx, bg);
    }
}
extern "C" fn tty_draw_pane(mut tty: *mut tty, mut ctx: *const tty_ctx, mut py: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut nx: u_int = unsafe { (*ctx).sx };
    let mut i: u_int = 0;
    let mut x: u_int = 0;
    let mut rx: u_int = 0;
    let mut ry: u_int = 0;
    (unsafe {
        log_debug(
            b"%s: %s %u %d\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_pane\0")).as_ptr(),
            (*(*tty).client).name,
            py,
            (*ctx).bigger,
        )
    });
    if (unsafe { (*ctx).bigger }) == 0 {
        tty_draw_line(
            tty,
            s, 0 as u_int,
            py,
            nx,
            unsafe { (*ctx).xoff },
            (unsafe { (*ctx).yoff }).wrapping_add(py),
            unsafe { &(*ctx).defaults },
            unsafe { (*ctx).palette },
        );
        return;
    }
    if tty_clamp_line(
        tty,
        ctx, 0 as u_int,
        py,
        nx,
        &mut i,
        &mut x,
        &mut rx,
        &mut ry,
    ) != 0
    {
        tty_draw_line(
            tty,
            s,
            i,
            py,
            rx,
            x,
            ry,
            unsafe { &(*ctx).defaults },
            unsafe { (*ctx).palette },
        );
    }
}
extern "C" fn tty_check_codeset(mut tty: *mut tty, mut gc: *const grid_cell) -> *const grid_cell {
    static mut new: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut c: i32 = 0;
    if (unsafe { (*gc).data.size }) as i32 == 1
        && ((unsafe { *((*gc).data.data).as_ptr() }) as i32) < 0x7f as i32
    {
        return gc;
    }
    if (unsafe { (*gc).flags }) as i32 & 0x80 as i32 != 0 {
        return gc;
    }
    if (unsafe { (*(*tty).client).flags }) & 0x10000 as i32 as u64 != 0 {
        return gc;
    }
    (unsafe {
        memcpy(
            &mut new as *mut grid_cell as *mut (),
            gc as *const (),
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    c = unsafe {
        tty_acs_reverse_get(
            tty,
            ((*gc).data.data).as_ptr() as *const i8,
            (*gc).data.size as size_t,
        )
    };
    if c != -(1 as i32) {
        (unsafe { utf8_set(&mut new.data, c as u_char) });
        (unsafe { new.attr = (new.attr as i32 | 0x80 as i32) as u_short });
        return unsafe { &mut new };
    }
    (unsafe { new.data.size = (*gc).data.width });
    if (unsafe { new.data.size }) as i32 > 21 {
        (unsafe { new.data.size = 21 as u_char });
    }
    (unsafe {
        memset(
            (new.data.data).as_mut_ptr() as *mut (),
            '_' as i32,
            new.data.size as u64,
        )
    });
    return unsafe { &mut new };
}
extern "C" fn tty_check_overlay(mut tty: *mut tty, mut px: u_int, mut py: u_int) -> i32 {
    let mut r: overlay_ranges = overlay_ranges {
        px: [0; 3],
        nx: [0; 3],
    };
    tty_check_overlay_range(tty, px, py, 1 as u_int, &mut r);
    if (r.nx[0 as i32 as usize]).wrapping_add(r.nx[1 as i32 as usize]) == 0 as u32 {
        return 0;
    }
    return 1;
}
extern "C" fn tty_check_overlay_range(
    mut tty: *mut tty,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut r: *mut overlay_ranges,
) {
    let mut c: *mut client = unsafe { (*tty).client };
    if unsafe { ((*c).overlay_check).is_none() } {
        (unsafe { (*r).px[0 as i32 as usize] = px });
        (unsafe { (*r).nx[0 as i32 as usize] = nx });
        (unsafe { (*r).px[1 as i32 as usize] = 0 as u_int });
        (unsafe { (*r).nx[1 as i32 as usize] = 0 as u_int });
        (unsafe { (*r).px[2 as i32 as usize] = 0 as u_int });
        (unsafe { (*r).nx[2 as i32 as usize] = 0 as u_int });
        return;
    }
    (unsafe {
        ((*c).overlay_check).expect("non-null function pointer")(
            c,
            (*c).overlay_data,
            px,
            py,
            nx,
            r,
        )
    });
}
#[no_mangle]
pub extern "C" fn tty_draw_line(
    mut tty: *mut tty,
    mut s: *mut screen,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut atx: u_int,
    mut aty: u_int,
    mut defaults: *const grid_cell,
    mut palette: *mut colour_palette,
) {
    let mut gd: *mut grid = unsafe { (*s).grid };
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut last: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut gcp: *const grid_cell = std::ptr::null::<grid_cell>();
    let mut gl: *mut grid_line = std::ptr::null_mut::<grid_line>();
    let mut c: *mut client = unsafe { (*tty).client };
    let mut r: overlay_ranges = overlay_ranges {
        px: [0; 3],
        nx: [0; 3],
    };
    let mut i: u_int = 0;
    let mut j: u_int = 0;
    let mut ux: u_int = 0;
    let mut sx: u_int = 0;
    let mut width: u_int = 0;
    let mut hidden: u_int = 0;
    let mut eux: u_int = 0;
    let mut nxx: u_int = 0;
    let mut cellsize: u_int = 0;
    let mut flags: i32 = 0;
    let mut cleared: i32 = 0;
    let mut wrapped: i32 = 0;
    let mut buf: [i8; 512] = [0; 512];
    let mut len: size_t = 0;
    (unsafe {
        log_debug(
            b"%s: px=%u py=%u nx=%u atx=%u aty=%u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0")).as_ptr(),
            px,
            py,
            nx,
            atx,
            aty,
        )
    });
    (unsafe {
        log_debug(
            b"%s: defaults: fg=%d, bg=%d\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0")).as_ptr(),
            (*defaults).fg,
            (*defaults).bg,
        )
    });
    flags = (unsafe { (*tty).flags }) & 0x1 as i32;
    (unsafe { (*tty).flags |= 0x1 as i32 });
    tty_update_mode(tty, unsafe { (*tty).mode }, s);
    tty_region_off(tty);
    tty_margin_off(tty);
    sx = unsafe { (*(*s).grid).sx };
    if nx > sx {
        nx = sx;
    }
    cellsize = unsafe { (*grid_get_line(gd, ((*gd).hsize).wrapping_add(py))).cellsize };
    if sx > cellsize {
        sx = cellsize;
    }
    if sx > (unsafe { (*tty).sx }) {
        sx = unsafe { (*tty).sx };
    }
    if sx > nx {
        sx = nx;
    }
    ux = 0 as u_int;
    if py == 0 as u32 {
        gl = std::ptr::null_mut::<grid_line>();
    } else {
        gl = unsafe {
            grid_get_line(
                gd,
                ((*gd).hsize).wrapping_add(py).wrapping_sub(1 as i32 as u32),
            )
        };
    }
    if gl.is_null()
        || !(unsafe { (*gl).flags }) & 0x1 as i32 != 0
        || atx != 0 as u32
        || (unsafe { (*tty).cx }) < (unsafe { (*tty).sx })
        || nx < (unsafe { (*tty).sx })
    {
        if nx < (unsafe { (*tty).sx })
            && atx == 0 as u32
            && px.wrapping_add(sx) != nx
            && (unsafe { tty_term_has((*tty).term, TTYC_EL1) }) != 0
            && tty_fake_bce(tty, defaults, 8 as u_int) == 0
            && (unsafe { ((*c).overlay_check).is_none() })
        {
            tty_default_attributes(tty, defaults, palette, 8 as u_int, unsafe {
                (*s).hyperlinks
            });
            tty_cursor(tty, nx.wrapping_sub(1 as i32 as u32), aty);
            tty_putcode(tty, TTYC_EL1);
            cleared = 1;
        }
    } else {
        (unsafe {
            log_debug(
                b"%s: wrapped line %u\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0")).as_ptr(),
                aty,
            )
        });
        wrapped = 1;
    }
    (unsafe {
        memcpy(
            &mut last as *mut grid_cell as *mut (),
            &grid_default_cell as *const grid_cell as *const (),
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    len = 0 as size_t;
    width = 0 as u_int;
    i = 0 as u_int;
    while i < sx {
        (unsafe { grid_view_get_cell(gd, px.wrapping_add(i), py, &mut gc) });
        gcp = tty_check_codeset(tty, &mut gc);
        if len != 0 as u64
            && (tty_check_overlay(tty, atx.wrapping_add(ux).wrapping_add(width), aty) == 0
                || (unsafe { (*gcp).attr }) as i32 & 0x80 as i32 != 0
                || (unsafe { (*gcp).flags }) as i32 != last.flags as i32
                || (unsafe { (*gcp).attr }) as i32 != last.attr as i32
                || (unsafe { (*gcp).fg }) != last.fg
                || (unsafe { (*gcp).bg }) != last.bg
                || (unsafe { (*gcp).us }) != last.us
                || (unsafe { (*gcp).link }) != last.link
                || ux
                    .wrapping_add(width)
                    .wrapping_add((unsafe { (*gcp).data.width }) as u32)
                    > nx
                || (::core::mem::size_of::<[i8; 512]>() as u64).wrapping_sub(len)
                    < (unsafe { (*gcp).data.size }) as u64)
        {
            tty_attributes(tty, &mut last, defaults, palette, unsafe {
                (*s).hyperlinks
            });
            if last.flags as i32 & 0x40 as i32 != 0 {
                (unsafe {
                    log_debug(
                        b"%s: %zu cleared\0" as *const u8 as *const i8,
                        (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0"))
                            .as_ptr(),
                        len,
                    )
                });
                tty_clear_line(
                    tty,
                    defaults,
                    aty,
                    atx.wrapping_add(ux),
                    width,
                    last.bg as u_int,
                );
            } else {
                if wrapped == 0 || atx != 0 as u32 || ux != 0 as u32 {
                    tty_cursor(tty, atx.wrapping_add(ux), aty);
                }
                tty_putn(tty, buf.as_mut_ptr() as *const (), len, width);
            }
            ux = (ux as u32).wrapping_add(width) as u_int as u_int;
            len = 0 as size_t;
            width = 0 as u_int;
            wrapped = 0;
        }
        if (unsafe { (*gcp).flags }) as i32 & 0x10 as i32 != 0 {
            (unsafe { screen_select_cell(s, &mut last, gcp) });
        } else {
            (unsafe {
                memcpy(
                    &mut last as *mut grid_cell as *mut (),
                    gcp as *const (),
                    ::core::mem::size_of::<grid_cell>() as u64,
                )
            });
        }
        tty_check_overlay_range(
            tty,
            atx.wrapping_add(ux),
            aty,
            (unsafe { (*gcp).data.width }) as u_int,
            &mut r,
        );
        hidden = 0 as u_int;
        j = 0 as u_int;
        while j < 3 as u32 {
            hidden = (hidden as u32).wrapping_add(r.nx[j as usize]) as u_int as u_int;
            j = j.wrapping_add(1);
        }
        hidden = ((unsafe { (*gcp).data.width }) as u32).wrapping_sub(hidden);
        if hidden != 0 as u32 && hidden == (unsafe { (*gcp).data.width }) as u32 {
            if !((unsafe { (*gcp).flags }) as i32) & 0x4 as i32 != 0 {
                ux = (ux as u32).wrapping_add((unsafe { (*gcp).data.width }) as u32) as u_int
                    as u_int;
            }
        } else if hidden != 0 as u32
            || ux.wrapping_add((unsafe { (*gcp).data.width }) as u32) > nx
        {
            if !((unsafe { (*gcp).flags }) as i32) & 0x4 as i32 != 0 {
                tty_attributes(tty, &mut last, defaults, palette, unsafe {
                    (*s).hyperlinks
                });
                j = 0 as u_int;
                while j < 3 as u32 {
                    if r.nx[j as usize] != 0 as u32 {
                        eux = (r.px[j as usize]).wrapping_sub(atx);
                        if eux < nx {
                            tty_cursor(tty, r.px[j as usize], aty);
                            nxx = nx.wrapping_sub(eux);
                            if r.nx[j as usize] > nxx {
                                r.nx[j as usize] = nxx;
                            }
                            tty_repeat_space(tty, r.nx[j as usize]);
                            ux = eux.wrapping_add(r.nx[j as usize]);
                        }
                    }
                    j = j.wrapping_add(1);
                }
            }
        } else if (unsafe { (*gcp).attr }) as i32 & 0x80 as i32 != 0 {
            tty_attributes(tty, &mut last, defaults, palette, unsafe {
                (*s).hyperlinks
            });
            tty_cursor(tty, atx.wrapping_add(ux), aty);
            j = 0 as u_int;
            while j < (unsafe { (*gcp).data.size }) as u32 {
                tty_putc(tty, unsafe { (*gcp).data.data[j as usize] });
                j = j.wrapping_add(1);
            }
            ux = (ux as u32).wrapping_add((unsafe { (*gcp).data.width }) as u32) as u_int as u_int;
        } else if !((unsafe { (*gcp).flags }) as i32) & 0x4 as i32 != 0 {
            (unsafe {
                memcpy(
                    buf.as_mut_ptr().offset(len as isize) as *mut (),
                    ((*gcp).data.data).as_ptr() as *const (),
                    (*gcp).data.size as u64,
                )
            });
            len =
                (len as u64).wrapping_add((unsafe { (*gcp).data.size }) as u64) as size_t as size_t;
            width = (width as u32).wrapping_add((unsafe { (*gcp).data.width }) as u32) as u_int
                as u_int;
        }
        i = i.wrapping_add(1);
    }
    if len != 0 as u64 && (!(last.flags as i32) & 0x40 as i32 != 0 || last.bg != 8) {
        tty_attributes(tty, &mut last, defaults, palette, unsafe {
            (*s).hyperlinks
        });
        if last.flags as i32 & 0x40 as i32 != 0 {
            (unsafe {
                log_debug(
                    b"%s: %zu cleared (end)\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0")).as_ptr(),
                    len,
                )
            });
            tty_clear_line(
                tty,
                defaults,
                aty,
                atx.wrapping_add(ux),
                width,
                last.bg as u_int,
            );
        } else {
            if wrapped == 0 || atx != 0 as u32 || ux != 0 as u32 {
                tty_cursor(tty, atx.wrapping_add(ux), aty);
            }
            tty_putn(tty, buf.as_mut_ptr() as *const (), len, width);
        }
        ux = (ux as u32).wrapping_add(width) as u_int as u_int;
    }
    if cleared == 0 && ux < nx {
        (unsafe {
            log_debug(
                b"%s: %u to end of line (%zu cleared)\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0")).as_ptr(),
                nx.wrapping_sub(ux),
                len,
            )
        });
        tty_default_attributes(tty, defaults, palette, 8 as u_int, unsafe {
            (*s).hyperlinks
        });
        tty_clear_line(
            tty,
            defaults,
            aty,
            atx.wrapping_add(ux),
            nx.wrapping_sub(ux), 8 as u_int,
        );
    }
    (unsafe { (*tty).flags = (*tty).flags & !(0x1 as i32) | flags });
    tty_update_mode(tty, unsafe { (*tty).mode }, s);
}
#[no_mangle]
pub extern "C" fn tty_sync_start(mut tty: *mut tty) {
    if (unsafe { (*tty).flags }) & 0x80 as i32 != 0 {
        return;
    }
    if (unsafe { (*tty).flags }) & 0x400 as i32 != 0 {
        return;
    }
    (unsafe { (*tty).flags |= 0x400 as i32 });
    if (unsafe { tty_term_has((*tty).term, TTYC_SYNC) }) != 0 {
        (unsafe {
            log_debug(
                b"%s sync start\0" as *const u8 as *const i8,
                (*(*tty).client).name,
            )
        });
        tty_putcode_i(tty, TTYC_SYNC, 1);
    }
}
#[no_mangle]
pub extern "C" fn tty_sync_end(mut tty: *mut tty) {
    if (unsafe { (*tty).flags }) & 0x80 as i32 != 0 {
        return;
    }
    if !(unsafe { (*tty).flags }) & 0x400 as i32 != 0 {
        return;
    }
    (unsafe { (*tty).flags &= !(0x400 as i32) });
    if (unsafe { tty_term_has((*tty).term, TTYC_SYNC) }) != 0 {
        (unsafe {
            log_debug(
                b"%s sync end\0" as *const u8 as *const i8,
                (*(*tty).client).name,
            )
        });
        tty_putcode_i(tty, TTYC_SYNC, 2);
    }
}
extern "C" fn tty_client_ready(mut ctx: *const tty_ctx, mut c: *mut client) -> i32 {
    if (unsafe { (*c).session }).is_null() || (unsafe { (*c).tty.term }).is_null() {
        return 0;
    }
    if (unsafe { (*c).flags }) & 0x40 as i32 as u64 != 0 {
        return 0;
    }
    if (unsafe { (*ctx).allow_invisible_panes }) != 0 {
        return 1;
    }
    if (unsafe { (*c).flags }) & 0x8 as i32 as u64 != 0 {
        return 0;
    }
    if (unsafe { (*c).tty.flags }) & 0x2 as i32 != 0 {
        return 0;
    }
    return 1;
}
#[no_mangle]
pub extern "C" fn tty_write(
    mut cmdfn: Option<unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()>,
    mut ctx: *mut tty_ctx,
) {
    let mut c: *mut client = std::ptr::null_mut::<client>();
    let mut state: i32 = 0;
    if unsafe { ((*ctx).set_client_cb).is_none() } {
        return;
    }
    c = unsafe { clients.tqh_first };
    while !c.is_null() {
        if tty_client_ready(ctx, c) != 0 {
            state = unsafe { ((*ctx).set_client_cb).expect("non-null function pointer")(ctx, c) };
            if state == -(1 as i32) {
                break;
            }
            if state != 0 {
                (unsafe { cmdfn.expect("non-null function pointer")(&mut (*c).tty, ctx) });
            }
        }
        c = unsafe { (*c).entry.tqe_next };
    }
}
#[no_mangle]
pub extern "C" fn tty_cmd_insertcharacter(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = unsafe { (*tty).client };
    if (unsafe { (*ctx).bigger }) != 0
        || !((unsafe { (*ctx).xoff }) == 0 as u32
            && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
        || tty_fake_bce(tty, unsafe { &(*ctx).defaults }, unsafe { (*ctx).bg }) != 0
        || (unsafe { tty_term_has((*tty).term, TTYC_ICH) }) == 0
            && (unsafe { tty_term_has((*tty).term, TTYC_ICH1) }) == 0
        || (unsafe { ((*c).overlay_check).is_some() })
    {
        tty_draw_pane(tty, ctx, unsafe { (*ctx).ocy });
        return;
    }
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_cursor_pane(tty, ctx, unsafe { (*ctx).ocx }, unsafe { (*ctx).ocy });
    tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, unsafe { (*ctx).num });
}
#[no_mangle]
pub extern "C" fn tty_cmd_deletecharacter(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = unsafe { (*tty).client };
    if (unsafe { (*ctx).bigger }) != 0
        || !((unsafe { (*ctx).xoff }) == 0 as u32
            && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
        || tty_fake_bce(tty, unsafe { &(*ctx).defaults }, unsafe { (*ctx).bg }) != 0
        || (unsafe { tty_term_has((*tty).term, TTYC_DCH) }) == 0
            && (unsafe { tty_term_has((*tty).term, TTYC_DCH1) }) == 0
        || (unsafe { ((*c).overlay_check).is_some() })
    {
        tty_draw_pane(tty, ctx, unsafe { (*ctx).ocy });
        return;
    }
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_cursor_pane(tty, ctx, unsafe { (*ctx).ocx }, unsafe { (*ctx).ocy });
    tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, unsafe { (*ctx).num });
}
#[no_mangle]
pub extern "C" fn tty_cmd_clearcharacter(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_clear_pane_line(
        tty,
        ctx,
        unsafe { (*ctx).ocy },
        unsafe { (*ctx).ocx },
        unsafe { (*ctx).num },
        unsafe { (*ctx).bg },
    );
}
#[no_mangle]
pub extern "C" fn tty_cmd_insertline(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = unsafe { (*tty).client };
    if (unsafe { (*ctx).bigger }) != 0
        || !((unsafe { (*ctx).xoff }) == 0 as u32
            && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
        || tty_fake_bce(tty, unsafe { &(*ctx).defaults }, unsafe { (*ctx).bg }) != 0
        || (unsafe { tty_term_has((*tty).term, TTYC_CSR) }) == 0
        || (unsafe { tty_term_has((*tty).term, TTYC_IL1) }) == 0
        || (unsafe { (*ctx).sx }) == 1 as u32
        || (unsafe { (*ctx).sy }) == 1 as u32
        || (unsafe { ((*c).overlay_check).is_some() })
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_region_pane(tty, ctx, unsafe { (*ctx).orupper }, unsafe {
        (*ctx).orlower
    });
    tty_margin_off(tty);
    tty_cursor_pane(tty, ctx, unsafe { (*ctx).ocx }, unsafe { (*ctx).ocy });
    tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, unsafe { (*ctx).num });
    (unsafe {
        (*tty).cy = (2147483647 as i32 as u32)
            .wrapping_mul(2 as u32)
            .wrapping_add(1 as u32)
    });
    (unsafe { (*tty).cx = (*tty).cy });
}
#[no_mangle]
pub extern "C" fn tty_cmd_deleteline(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = unsafe { (*tty).client };
    if (unsafe { (*ctx).bigger }) != 0
        || !((unsafe { (*ctx).xoff }) == 0 as u32
            && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
        || tty_fake_bce(tty, unsafe { &(*ctx).defaults }, unsafe { (*ctx).bg }) != 0
        || (unsafe { tty_term_has((*tty).term, TTYC_CSR) }) == 0
        || (unsafe { tty_term_has((*tty).term, TTYC_DL1) }) == 0
        || (unsafe { (*ctx).sx }) == 1 as u32
        || (unsafe { (*ctx).sy }) == 1 as u32
        || (unsafe { ((*c).overlay_check).is_some() })
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_region_pane(tty, ctx, unsafe { (*ctx).orupper }, unsafe {
        (*ctx).orlower
    });
    tty_margin_off(tty);
    tty_cursor_pane(tty, ctx, unsafe { (*ctx).ocx }, unsafe { (*ctx).ocy });
    tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, unsafe { (*ctx).num });
    (unsafe {
        (*tty).cy = (2147483647 as i32 as u32)
            .wrapping_mul(2 as u32)
            .wrapping_add(1 as u32)
    });
    (unsafe { (*tty).cx = (*tty).cy });
}
#[no_mangle]
pub extern "C" fn tty_cmd_clearline(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_clear_pane_line(
        tty,
        ctx,
        unsafe { (*ctx).ocy }, 0 as u_int,
        unsafe { (*ctx).sx },
        unsafe { (*ctx).bg },
    );
}
#[no_mangle]
pub extern "C" fn tty_cmd_clearendofline(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut nx: u_int = (unsafe { (*ctx).sx }).wrapping_sub(unsafe { (*ctx).ocx });
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_clear_pane_line(
        tty,
        ctx,
        unsafe { (*ctx).ocy },
        unsafe { (*ctx).ocx },
        nx,
        unsafe { (*ctx).bg },
    );
}
#[no_mangle]
pub extern "C" fn tty_cmd_clearstartofline(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_clear_pane_line(
        tty,
        ctx,
        unsafe { (*ctx).ocy }, 0 as u_int,
        (unsafe { (*ctx).ocx }).wrapping_add(1 as i32 as u32),
        unsafe { (*ctx).bg },
    );
}
#[no_mangle]
pub extern "C" fn tty_cmd_reverseindex(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = unsafe { (*tty).client };
    if (unsafe { (*ctx).ocy }) != (unsafe { (*ctx).orupper }) {
        return;
    }
    if (unsafe { (*ctx).bigger }) != 0
        || !((unsafe { (*ctx).xoff }) == 0 as u32
            && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
            && (unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0
        || tty_fake_bce(tty, unsafe { &(*ctx).defaults }, 8 as u_int) != 0
        || (unsafe { tty_term_has((*tty).term, TTYC_CSR) }) == 0
        || (unsafe { tty_term_has((*tty).term, TTYC_RI) }) == 0
            && (unsafe { tty_term_has((*tty).term, TTYC_RIN) }) == 0
        || (unsafe { (*ctx).sx }) == 1 as u32
        || (unsafe { (*ctx).sy }) == 1 as u32
        || (unsafe { ((*c).overlay_check).is_some() })
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_region_pane(tty, ctx, unsafe { (*ctx).orupper }, unsafe {
        (*ctx).orlower
    });
    tty_margin_pane(tty, ctx);
    tty_cursor_pane(tty, ctx, unsafe { (*ctx).ocx }, unsafe { (*ctx).orupper });
    if (unsafe { tty_term_has((*tty).term, TTYC_RI) }) != 0 {
        tty_putcode(tty, TTYC_RI);
    } else {
        tty_putcode_i(tty, TTYC_RIN, 1);
    };
}
#[no_mangle]
pub extern "C" fn tty_cmd_linefeed(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = unsafe { (*tty).client };
    if (unsafe { (*ctx).ocy }) != (unsafe { (*ctx).orlower }) {
        return;
    }
    if (unsafe { (*ctx).bigger }) != 0
        || !((unsafe { (*ctx).xoff }) == 0 as u32
            && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
            && (unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0
        || tty_fake_bce(tty, unsafe { &(*ctx).defaults }, 8 as u_int) != 0
        || (unsafe { tty_term_has((*tty).term, TTYC_CSR) }) == 0
        || (unsafe { (*ctx).sx }) == 1 as u32
        || (unsafe { (*ctx).sy }) == 1 as u32
        || (unsafe { ((*c).overlay_check).is_some() })
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_region_pane(tty, ctx, unsafe { (*ctx).orupper }, unsafe {
        (*ctx).orlower
    });
    tty_margin_pane(tty, ctx);
    if (unsafe { (*ctx).xoff }).wrapping_add(unsafe { (*ctx).ocx }) > (unsafe { (*tty).rright }) {
        if (unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0 {
            tty_cursor(
                tty, 0 as u_int,
                (unsafe { (*ctx).yoff }).wrapping_add(unsafe { (*ctx).ocy }),
            );
        } else {
            tty_cursor(
                tty,
                unsafe { (*tty).rright },
                (unsafe { (*ctx).yoff }).wrapping_add(unsafe { (*ctx).ocy }),
            );
        }
    } else {
        tty_cursor_pane(tty, ctx, unsafe { (*ctx).ocx }, unsafe { (*ctx).ocy });
    }
    tty_putc(tty, '\n' as i32 as u_char);
}
#[no_mangle]
pub extern "C" fn tty_cmd_scrollup(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = unsafe { (*tty).client };
    let mut i: u_int = 0;
    if (unsafe { (*ctx).bigger }) != 0
        || !((unsafe { (*ctx).xoff }) == 0 as u32
            && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
            && (unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0
        || tty_fake_bce(tty, unsafe { &(*ctx).defaults }, 8 as u_int) != 0
        || (unsafe { tty_term_has((*tty).term, TTYC_CSR) }) == 0
        || (unsafe { (*ctx).sx }) == 1 as u32
        || (unsafe { (*ctx).sy }) == 1 as u32
        || (unsafe { ((*c).overlay_check).is_some() })
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_region_pane(tty, ctx, unsafe { (*ctx).orupper }, unsafe {
        (*ctx).orlower
    });
    tty_margin_pane(tty, ctx);
    if (unsafe { (*ctx).num }) == 1 as u32
        || (unsafe { tty_term_has((*tty).term, TTYC_INDN) }) == 0
    {
        if (unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0 {
            tty_cursor(tty, 0 as u_int, unsafe { (*tty).rlower });
        } else {
            tty_cursor(tty, unsafe { (*tty).rright }, unsafe { (*tty).rlower });
        }
        i = 0 as u_int;
        while i < (unsafe { (*ctx).num }) {
            tty_putc(tty, '\n' as i32 as u_char);
            i = i.wrapping_add(1);
        }
    } else {
        if (unsafe { (*tty).cy })
            == (2147483647 as i32 as u32)
                .wrapping_mul(2 as u32)
                .wrapping_add(1 as u32)
        {
            tty_cursor(tty, 0 as u_int, 0 as u_int);
        } else {
            tty_cursor(tty, 0 as u_int, unsafe { (*tty).cy });
        }
        tty_putcode_i(tty, TTYC_INDN, (unsafe { (*ctx).num }) as i32);
    };
}
#[no_mangle]
pub extern "C" fn tty_cmd_scrolldown(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut i: u_int = 0;
    let mut c: *mut client = unsafe { (*tty).client };
    if (unsafe { (*ctx).bigger }) != 0
        || !((unsafe { (*ctx).xoff }) == 0 as u32
            && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
            && (unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0
        || tty_fake_bce(tty, unsafe { &(*ctx).defaults }, 8 as u_int) != 0
        || (unsafe { tty_term_has((*tty).term, TTYC_CSR) }) == 0
        || (unsafe { tty_term_has((*tty).term, TTYC_RI) }) == 0
            && (unsafe { tty_term_has((*tty).term, TTYC_RIN) }) == 0
        || (unsafe { (*ctx).sx }) == 1 as u32
        || (unsafe { (*ctx).sy }) == 1 as u32
        || (unsafe { ((*c).overlay_check).is_some() })
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_region_pane(tty, ctx, unsafe { (*ctx).orupper }, unsafe {
        (*ctx).orlower
    });
    tty_margin_pane(tty, ctx);
    tty_cursor_pane(tty, ctx, unsafe { (*ctx).ocx }, unsafe { (*ctx).orupper });
    if (unsafe { tty_term_has((*tty).term, TTYC_RIN) }) != 0 {
        tty_putcode_i(tty, TTYC_RIN, (unsafe { (*ctx).num }) as i32);
    } else {
        i = 0 as u_int;
        while i < (unsafe { (*ctx).num }) {
            tty_putcode(tty, TTYC_RI);
            i = i.wrapping_add(1);
        }
    };
}
#[no_mangle]
pub extern "C" fn tty_cmd_clearendofscreen(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut nx: u_int = 0;
    let mut ny: u_int = 0;
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_region_pane(
        tty,
        ctx, 0 as u_int,
        (unsafe { (*ctx).sy }).wrapping_sub(1 as i32 as u32),
    );
    tty_margin_off(tty);
    px = 0 as u_int;
    nx = unsafe { (*ctx).sx };
    py = (unsafe { (*ctx).ocy }).wrapping_add(1 as i32 as u32);
    ny = (unsafe { (*ctx).sy })
        .wrapping_sub(unsafe { (*ctx).ocy })
        .wrapping_sub(1 as i32 as u32);
    tty_clear_pane_area(tty, ctx, py, ny, px, nx, unsafe { (*ctx).bg });
    px = unsafe { (*ctx).ocx };
    nx = (unsafe { (*ctx).sx }).wrapping_sub(unsafe { (*ctx).ocx });
    py = unsafe { (*ctx).ocy };
    tty_clear_pane_line(tty, ctx, py, px, nx, unsafe { (*ctx).bg });
}
#[no_mangle]
pub extern "C" fn tty_cmd_clearstartofscreen(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut nx: u_int = 0;
    let mut ny: u_int = 0;
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_region_pane(
        tty,
        ctx, 0 as u_int,
        (unsafe { (*ctx).sy }).wrapping_sub(1 as i32 as u32),
    );
    tty_margin_off(tty);
    px = 0 as u_int;
    nx = unsafe { (*ctx).sx };
    py = 0 as u_int;
    ny = unsafe { (*ctx).ocy };
    tty_clear_pane_area(tty, ctx, py, ny, px, nx, unsafe { (*ctx).bg });
    px = 0 as u_int;
    nx = (unsafe { (*ctx).ocx }).wrapping_add(1 as i32 as u32);
    py = unsafe { (*ctx).ocy };
    tty_clear_pane_line(tty, ctx, py, px, nx, unsafe { (*ctx).bg });
}
#[no_mangle]
pub extern "C" fn tty_cmd_clearscreen(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut nx: u_int = 0;
    let mut ny: u_int = 0;
    tty_default_attributes(
        tty,
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*ctx).bg },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_region_pane(
        tty,
        ctx, 0 as u_int,
        (unsafe { (*ctx).sy }).wrapping_sub(1 as i32 as u32),
    );
    tty_margin_off(tty);
    px = 0 as u_int;
    nx = unsafe { (*ctx).sx };
    py = 0 as u_int;
    ny = unsafe { (*ctx).sy };
    tty_clear_pane_area(tty, ctx, py, ny, px, nx, unsafe { (*ctx).bg });
}
#[no_mangle]
pub extern "C" fn tty_cmd_alignmenttest(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut i: u_int = 0;
    let mut j: u_int = 0;
    if (unsafe { (*ctx).bigger }) != 0 {
        (unsafe { ((*ctx).redraw_cb).expect("non-null function pointer")(ctx) });
        return;
    }
    tty_attributes(
        tty,
        unsafe { &grid_default_cell },
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    tty_region_pane(
        tty,
        ctx, 0 as u_int,
        (unsafe { (*ctx).sy }).wrapping_sub(1 as i32 as u32),
    );
    tty_margin_off(tty);
    j = 0 as u_int;
    while j < (unsafe { (*ctx).sy }) {
        tty_cursor_pane(tty, ctx, 0 as u_int, j);
        i = 0 as u_int;
        while i < (unsafe { (*ctx).sx }) {
            tty_putc(tty, 'E' as i32 as u_char);
            i = i.wrapping_add(1);
        }
        j = j.wrapping_add(1);
    }
}
#[no_mangle]
pub extern "C" fn tty_cmd_cell(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut gcp: *const grid_cell = unsafe { (*ctx).cell };
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut r: overlay_ranges = overlay_ranges {
        px: [0; 3],
        nx: [0; 3],
    };
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut i: u_int = 0;
    let mut vis: u_int = 0 as u_int;
    px = (unsafe { (*ctx).xoff })
        .wrapping_add(unsafe { (*ctx).ocx })
        .wrapping_sub(unsafe { (*ctx).wox });
    py = (unsafe { (*ctx).yoff })
        .wrapping_add(unsafe { (*ctx).ocy })
        .wrapping_sub(unsafe { (*ctx).woy });
    if tty_is_visible(
        tty,
        ctx,
        unsafe { (*ctx).ocx },
        unsafe { (*ctx).ocy }, 1 as u_int, 1 as u_int,
    ) == 0
        || (unsafe { (*gcp).data.width }) as i32 == 1 && tty_check_overlay(tty, px, py) == 0
    {
        return;
    }
    if (unsafe { (*gcp).data.width }) as i32 > 1 {
        tty_check_overlay_range(tty, px, py, (unsafe { (*gcp).data.width }) as u_int, &mut r);
        i = 0 as u_int;
        while i < 3 as u32 {
            vis = (vis as u32).wrapping_add(r.nx[i as usize]) as u_int as u_int;
            i = i.wrapping_add(1);
        }
        if vis < (unsafe { (*gcp).data.width }) as u32 {
            tty_draw_line(
                tty,
                s,
                unsafe { (*s).cx },
                unsafe { (*s).cy },
                (unsafe { (*gcp).data.width }) as u_int,
                px,
                py,
                unsafe { &(*ctx).defaults },
                unsafe { (*ctx).palette },
            );
            return;
        }
    }
    if (unsafe { (*ctx).xoff })
        .wrapping_add(unsafe { (*ctx).ocx })
        .wrapping_sub(unsafe { (*ctx).wox })
        > (unsafe { (*tty).sx }).wrapping_sub(1 as i32 as u32)
        && (unsafe { (*ctx).ocy }) == (unsafe { (*ctx).orlower })
        && ((unsafe { (*ctx).xoff }) == 0 as u32
            && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
    {
        tty_region_pane(tty, ctx, unsafe { (*ctx).orupper }, unsafe {
            (*ctx).orlower
        });
    }
    tty_margin_off(tty);
    tty_cursor_pane_unless_wrap(tty, ctx, unsafe { (*ctx).ocx }, unsafe { (*ctx).ocy });
    tty_cell(
        tty,
        unsafe { (*ctx).cell },
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    if (unsafe { (*ctx).num }) == 1 as u32 {
        tty_invalidate(tty);
    }
}
#[no_mangle]
pub extern "C" fn tty_cmd_cells(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut r: overlay_ranges = overlay_ranges {
        px: [0; 3],
        nx: [0; 3],
    };
    let mut i: u_int = 0;
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut cx: u_int = 0;
    let mut cp: *mut i8 = (unsafe { (*ctx).ptr }) as *mut i8;
    if tty_is_visible(
        tty,
        ctx,
        unsafe { (*ctx).ocx },
        unsafe { (*ctx).ocy },
        unsafe { (*ctx).num }, 1 as u_int,
    ) == 0
    {
        return;
    }
    if (unsafe { (*ctx).bigger }) != 0
        && ((unsafe { (*ctx).xoff }).wrapping_add(unsafe { (*ctx).ocx }) < (unsafe { (*ctx).wox })
            || (unsafe { (*ctx).xoff })
                .wrapping_add(unsafe { (*ctx).ocx })
                .wrapping_add(unsafe { (*ctx).num })
                > (unsafe { (*ctx).wox }).wrapping_add(unsafe { (*ctx).wsx }))
    {
        if (unsafe { (*ctx).wrapped }) == 0
            || !((unsafe { (*ctx).xoff }) == 0 as u32
                && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
            || (unsafe { (*(*tty).term).flags }) & 0x2 as i32 != 0
            || (unsafe { (*ctx).xoff }).wrapping_add(unsafe { (*ctx).ocx }) != 0 as u32
            || (unsafe { (*ctx).yoff }).wrapping_add(unsafe { (*ctx).ocy })
                != (unsafe { (*tty).cy }).wrapping_add(1 as i32 as u32)
            || (unsafe { (*tty).cx }) < (unsafe { (*tty).sx })
            || (unsafe { (*tty).cy }) == (unsafe { (*tty).rlower })
        {
            tty_draw_pane(tty, ctx, unsafe { (*ctx).ocy });
        } else {
            (unsafe { ((*ctx).redraw_cb).expect("non-null function pointer")(ctx) });
        }
        return;
    }
    tty_margin_off(tty);
    tty_cursor_pane_unless_wrap(tty, ctx, unsafe { (*ctx).ocx }, unsafe { (*ctx).ocy });
    tty_attributes(
        tty,
        unsafe { (*ctx).cell },
        unsafe { &(*ctx).defaults },
        unsafe { (*ctx).palette },
        unsafe { (*(*ctx).s).hyperlinks },
    );
    px = (unsafe { (*ctx).xoff })
        .wrapping_add(unsafe { (*ctx).ocx })
        .wrapping_sub(unsafe { (*ctx).wox });
    py = (unsafe { (*ctx).yoff })
        .wrapping_add(unsafe { (*ctx).ocy })
        .wrapping_sub(unsafe { (*ctx).woy });
    tty_check_overlay_range(tty, px, py, unsafe { (*ctx).num }, &mut r);
    i = 0 as u_int;
    while i < 3 as u32 {
        if r.nx[i as usize] != 0 as u32 {
            cx = (r.px[i as usize])
                .wrapping_sub(unsafe { (*ctx).xoff })
                .wrapping_add(unsafe { (*ctx).wox });
            tty_cursor_pane_unless_wrap(tty, ctx, cx, unsafe { (*ctx).ocy });
            tty_putn(
                tty,
                (unsafe { cp.offset(r.px[i as usize] as isize).offset(-(px as isize)) })
                    as *const (),
                r.nx[i as usize] as size_t,
                r.nx[i as usize],
            );
        }
        i = i.wrapping_add(1);
    }
}
#[no_mangle]
pub extern "C" fn tty_cmd_setselection(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    tty_set_selection(
        tty,
        (unsafe { (*ctx).ptr2 }) as *const i8,
        (unsafe { (*ctx).ptr }) as *const i8,
        (unsafe { (*ctx).num }) as size_t,
    );
}
#[no_mangle]
pub extern "C" fn tty_set_selection(
    mut tty: *mut tty,
    mut flags: *const i8,
    mut buf: *const i8,
    mut len: size_t,
) {
    let mut encoded: *mut i8 = std::ptr::null_mut::<i8>();
    let mut size: size_t = 0;
    if !(unsafe { (*tty).flags }) & 0x10 as i32 != 0 {
        return;
    }
    if (unsafe { tty_term_has((*tty).term, TTYC_MS) }) == 0 {
        return;
    }
    size = (4 as i32 as u64)
        .wrapping_mul(
            len.wrapping_add(2 as i32 as u64)
                .wrapping_div(3 as i32 as u64),
        )
        .wrapping_add(1 as i32 as u64);
    encoded = (unsafe { xmalloc(size) }) as *mut i8;
    (unsafe { __b64_ntop(buf as *const u8, len, encoded, size) });
    (unsafe { (*tty).flags |= 0x8 as i32 });
    tty_putcode_ss(tty, TTYC_MS, flags, encoded);
    (unsafe { free(encoded as *mut ()) });
}
#[no_mangle]
pub extern "C" fn tty_cmd_rawstring(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    (unsafe { (*tty).flags |= 0x8 as i32 });
    tty_add(
        tty,
        (unsafe { (*ctx).ptr }) as *const i8,
        (unsafe { (*ctx).num }) as size_t,
    );
    tty_invalidate(tty);
}
#[no_mangle]
pub extern "C" fn tty_cmd_syncstart(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    if (unsafe { (*ctx).num }) == 0x11 as i32 as u32 {
        tty_sync_start(tty);
    } else if !(unsafe { (*ctx).num }) & 0x10 as i32 as u32 != 0 {
        if (unsafe { (*ctx).num }) != 0 || (unsafe { ((*(*tty).client).overlay_draw).is_some() }) {
            tty_sync_start(tty);
        }
    }
}
#[no_mangle]
pub extern "C" fn tty_cell(
    mut tty: *mut tty,
    mut gc: *const grid_cell,
    mut defaults: *const grid_cell,
    mut palette: *mut colour_palette,
    mut hl: *mut hyperlinks,
) {
    let mut gcp: *const grid_cell = std::ptr::null::<grid_cell>();
    if (unsafe { (*(*tty).term).flags }) & 0x2 as i32 != 0
        && (unsafe { (*tty).cy }) == (unsafe { (*tty).sy }).wrapping_sub(1 as i32 as u32)
        && (unsafe { (*tty).cx }) == (unsafe { (*tty).sx }).wrapping_sub(1 as i32 as u32)
    {
        return;
    }
    if (unsafe { (*gc).flags }) as i32 & 0x4 as i32 != 0 {
        return;
    }
    gcp = tty_check_codeset(tty, gc);
    tty_attributes(tty, gcp, defaults, palette, hl);
    if (unsafe { (*gcp).data.size }) as i32 == 1 {
        tty_attributes(tty, gcp, defaults, palette, hl);
        if ((unsafe { *((*gcp).data.data).as_ptr() }) as i32) < 0x20 as i32
            || (unsafe { *((*gcp).data.data).as_ptr() }) as i32 == 0x7f as i32
        {
            return;
        }
        tty_putc(tty, unsafe { *((*gcp).data.data).as_ptr() });
        return;
    }
    tty_putn(
        tty,
        (unsafe { ((*gcp).data.data).as_ptr() }) as *const (),
        (unsafe { (*gcp).data.size }) as size_t,
        (unsafe { (*gcp).data.width }) as u_int,
    );
}
#[no_mangle]
pub extern "C" fn tty_reset(mut tty: *mut tty) {
    let mut gc: *mut grid_cell = unsafe { &mut (*tty).cell };
    if (unsafe { grid_cells_equal(gc, &grid_default_cell) }) == 0 {
        if (unsafe { (*gc).link }) != 0 as u32 {
            tty_putcode_ss(
                tty,
                TTYC_HLS,
                b"\0" as *const u8 as *const i8,
                b"\0" as *const u8 as *const i8,
            );
        }
        if (unsafe { (*gc).attr }) as i32 & 0x80 as i32 != 0
            && (unsafe { tty_acs_needed(tty) }) != 0
        {
            tty_putcode(tty, TTYC_RMACS);
        }
        tty_putcode(tty, TTYC_SGR0);
        (unsafe {
            memcpy(
                gc as *mut (),
                &grid_default_cell as *const grid_cell as *const (),
                ::core::mem::size_of::<grid_cell>() as u64,
            )
        });
    }
    (unsafe {
        memcpy(
            &mut (*tty).last_cell as *mut grid_cell as *mut (),
            &grid_default_cell as *const grid_cell as *const (),
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
}
#[no_mangle]
pub extern "C" fn tty_invalidate(mut tty: *mut tty) {
    (unsafe {
        memcpy(
            &mut (*tty).cell as *mut grid_cell as *mut (),
            &grid_default_cell as *const grid_cell as *const (),
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    (unsafe {
        memcpy(
            &mut (*tty).last_cell as *mut grid_cell as *mut (),
            &grid_default_cell as *const grid_cell as *const (),
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    (unsafe {
        (*tty).cy = (2147483647 as i32 as u32)
            .wrapping_mul(2 as u32)
            .wrapping_add(1 as u32)
    });
    (unsafe { (*tty).cx = (*tty).cy });
    (unsafe {
        (*tty).rleft = (2147483647 as i32 as u32)
            .wrapping_mul(2 as u32)
            .wrapping_add(1 as u32)
    });
    (unsafe { (*tty).rupper = (*tty).rleft });
    (unsafe {
        (*tty).rright = (2147483647 as i32 as u32)
            .wrapping_mul(2 as u32)
            .wrapping_add(1 as u32)
    });
    (unsafe { (*tty).rlower = (*tty).rright });
    if (unsafe { (*tty).flags }) & 0x10 as i32 != 0 {
        if (unsafe { (*(*tty).term).flags }) & 0x4 as i32 != 0 {
            tty_putcode(tty, TTYC_ENMG);
        }
        tty_putcode(tty, TTYC_SGR0);
        (unsafe { (*tty).mode = 0xffffff as i32 });
        tty_update_mode(tty, 0x1 as i32, std::ptr::null_mut::<screen>());
        tty_cursor(tty, 0 as u_int, 0 as u_int);
        tty_region_off(tty);
        tty_margin_off(tty);
    } else {
        (unsafe { (*tty).mode = 0x1 as i32 });
    };
}
#[no_mangle]
pub extern "C" fn tty_region_off(mut tty: *mut tty) {
    tty_region(
        tty, 0 as u_int,
        (unsafe { (*tty).sy }).wrapping_sub(1 as i32 as u32),
    );
}
extern "C" fn tty_region_pane(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut rupper: u_int,
    mut rlower: u_int,
) {
    tty_region(
        tty,
        (unsafe { (*ctx).yoff })
            .wrapping_add(rupper)
            .wrapping_sub(unsafe { (*ctx).woy }),
        (unsafe { (*ctx).yoff })
            .wrapping_add(rlower)
            .wrapping_sub(unsafe { (*ctx).woy }),
    );
}
extern "C" fn tty_region(mut tty: *mut tty, mut rupper: u_int, mut rlower: u_int) {
    if (unsafe { (*tty).rlower }) == rlower && (unsafe { (*tty).rupper }) == rupper {
        return;
    }
    if (unsafe { tty_term_has((*tty).term, TTYC_CSR) }) == 0 {
        return;
    }
    (unsafe { (*tty).rupper = rupper });
    (unsafe { (*tty).rlower = rlower });
    if (unsafe { (*tty).cx }) >= (unsafe { (*tty).sx }) {
        if (unsafe { (*tty).cy })
            == (2147483647 as i32 as u32)
                .wrapping_mul(2 as u32)
                .wrapping_add(1 as u32)
        {
            tty_cursor(tty, 0 as u_int, 0 as u_int);
        } else {
            tty_cursor(tty, 0 as u_int, unsafe { (*tty).cy });
        }
    }
    tty_putcode_ii(
        tty,
        TTYC_CSR,
        (unsafe { (*tty).rupper }) as i32,
        (unsafe { (*tty).rlower }) as i32,
    );
    (unsafe {
        (*tty).cy = (2147483647 as i32 as u32)
            .wrapping_mul(2 as u32)
            .wrapping_add(1 as u32)
    });
    (unsafe { (*tty).cx = (*tty).cy });
}
#[no_mangle]
pub extern "C" fn tty_margin_off(mut tty: *mut tty) {
    tty_margin(
        tty, 0 as u_int,
        (unsafe { (*tty).sx }).wrapping_sub(1 as i32 as u32),
    );
}
extern "C" fn tty_margin_pane(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    tty_margin(
        tty,
        (unsafe { (*ctx).xoff }).wrapping_sub(unsafe { (*ctx).wox }),
        (unsafe { (*ctx).xoff })
            .wrapping_add(unsafe { (*ctx).sx })
            .wrapping_sub(1 as i32 as u32)
            .wrapping_sub(unsafe { (*ctx).wox }),
    );
}
extern "C" fn tty_margin(mut tty: *mut tty, mut rleft: u_int, mut rright: u_int) {
    if (unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0 {
        return;
    }
    if (unsafe { (*tty).rleft }) == rleft && (unsafe { (*tty).rright }) == rright {
        return;
    }
    tty_putcode_ii(
        tty,
        TTYC_CSR,
        (unsafe { (*tty).rupper }) as i32,
        (unsafe { (*tty).rlower }) as i32,
    );
    (unsafe { (*tty).rleft = rleft });
    (unsafe { (*tty).rright = rright });
    if rleft == 0 as u32 && rright == (unsafe { (*tty).sx }).wrapping_sub(1 as i32 as u32) {
        tty_putcode(tty, TTYC_CLMG);
    } else {
        tty_putcode_ii(tty, TTYC_CMG, rleft as i32, rright as i32);
    }
    (unsafe {
        (*tty).cy = (2147483647 as i32 as u32)
            .wrapping_mul(2 as u32)
            .wrapping_add(1 as u32)
    });
    (unsafe { (*tty).cx = (*tty).cy });
}
extern "C" fn tty_cursor_pane_unless_wrap(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut cx: u_int,
    mut cy: u_int,
) {
    if (unsafe { (*ctx).wrapped }) == 0
        || !((unsafe { (*ctx).xoff }) == 0 as u32
            && (unsafe { (*ctx).sx }) >= (unsafe { (*tty).sx }))
        || (unsafe { (*(*tty).term).flags }) & 0x2 as i32 != 0
        || (unsafe { (*ctx).xoff }).wrapping_add(cx) != 0 as u32
        || (unsafe { (*ctx).yoff }).wrapping_add(cy)
            != (unsafe { (*tty).cy }).wrapping_add(1 as i32 as u32)
        || (unsafe { (*tty).cx }) < (unsafe { (*tty).sx })
        || (unsafe { (*tty).cy }) == (unsafe { (*tty).rlower })
    {
        tty_cursor_pane(tty, ctx, cx, cy);
    } else {
        (unsafe {
            log_debug(
                b"%s: will wrap at %u,%u\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 28], &[i8; 28]>(b"tty_cursor_pane_unless_wrap\0"))
                    .as_ptr(),
                (*tty).cx,
                (*tty).cy,
            )
        });
    };
}
extern "C" fn tty_cursor_pane(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut cx: u_int,
    mut cy: u_int,
) {
    tty_cursor(
        tty,
        (unsafe { (*ctx).xoff })
            .wrapping_add(cx)
            .wrapping_sub(unsafe { (*ctx).wox }),
        (unsafe { (*ctx).yoff })
            .wrapping_add(cy)
            .wrapping_sub(unsafe { (*ctx).woy }),
    );
}
#[no_mangle]
pub extern "C" fn tty_cursor(mut tty: *mut tty, mut cx: u_int, mut cy: u_int) {
    let mut current_block: u64;
    let mut term: *mut tty_term = unsafe { (*tty).term };
    let mut thisx: u_int = 0;
    let mut thisy: u_int = 0;
    let mut change: i32 = 0;
    if (unsafe { (*tty).flags }) & 0x80 as i32 != 0 {
        return;
    }
    thisx = unsafe { (*tty).cx };
    thisy = unsafe { (*tty).cy };
    if cx == thisx && cy == thisy && cx == (unsafe { (*tty).sx }) {
        return;
    }
    if cx > (unsafe { (*tty).sx }).wrapping_sub(1 as i32 as u32) {
        (unsafe {
            log_debug(
                b"%s: x too big %u > %u\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 11], &[i8; 11]>(b"tty_cursor\0")).as_ptr(),
                cx,
                ((*tty).sx).wrapping_sub(1 as i32 as u32),
            )
        });
        cx = (unsafe { (*tty).sx }).wrapping_sub(1 as i32 as u32);
    }
    if cx == thisx && cy == thisy {
        return;
    }
    if thisx > (unsafe { (*tty).sx }).wrapping_sub(1 as i32 as u32) {
        current_block = 16274275253645873319;
    } else if cx == 0 as u32
        && cy == 0 as u32
        && (unsafe { tty_term_has(term, TTYC_HOME) }) != 0
    {
        tty_putcode(tty, TTYC_HOME);
        current_block = 17422582042514589789;
    } else if cx == 0 as u32
        && cy == thisy.wrapping_add(1 as i32 as u32)
        && thisy != (unsafe { (*tty).rlower })
        && ((unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0
            || (unsafe { (*tty).rleft }) == 0 as u32)
    {
        tty_putc(tty, '\r' as i32 as u_char);
        tty_putc(tty, '\n' as i32 as u_char);
        current_block = 17422582042514589789;
    } else if cy == thisy {
        if cx == 0 as u32
            && ((unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0
                || (unsafe { (*tty).rleft }) == 0 as u32)
        {
            tty_putc(tty, '\r' as i32 as u_char);
            current_block = 17422582042514589789;
        } else if cx == thisx.wrapping_sub(1 as i32 as u32)
            && (unsafe { tty_term_has(term, TTYC_CUB1) }) != 0
        {
            tty_putcode(tty, TTYC_CUB1);
            current_block = 17422582042514589789;
        } else if cx == thisx.wrapping_add(1 as i32 as u32)
            && (unsafe { tty_term_has(term, TTYC_CUF1) }) != 0
        {
            tty_putcode(tty, TTYC_CUF1);
            current_block = 17422582042514589789;
        } else {
            change = thisx.wrapping_sub(cx) as i32;
            if (unsafe { abs(change) }) as u_int > cx
                && (unsafe { tty_term_has(term, TTYC_HPA) }) != 0
            {
                tty_putcode_i(tty, TTYC_HPA, cx as i32);
                current_block = 17422582042514589789;
            } else if change > 0
                && (unsafe { tty_term_has(term, TTYC_CUB) }) != 0
                && (unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0
            {
                if change == 2 && (unsafe { tty_term_has(term, TTYC_CUB1) }) != 0 {
                    tty_putcode(tty, TTYC_CUB1);
                    tty_putcode(tty, TTYC_CUB1);
                } else {
                    tty_putcode_i(tty, TTYC_CUB, change);
                }
                current_block = 17422582042514589789;
            } else if change < 0
                && (unsafe { tty_term_has(term, TTYC_CUF) }) != 0
                && (unsafe { (*(*tty).term).flags }) & 0x4 as i32 == 0
            {
                tty_putcode_i(tty, TTYC_CUF, -change);
                current_block = 17422582042514589789;
            } else {
                current_block = 16274275253645873319;
            }
        }
    } else if cx == thisx {
        if thisy != (unsafe { (*tty).rupper })
            && cy == thisy.wrapping_sub(1 as i32 as u32)
            && (unsafe { tty_term_has(term, TTYC_CUU1) }) != 0
        {
            tty_putcode(tty, TTYC_CUU1);
            current_block = 17422582042514589789;
        } else if thisy != (unsafe { (*tty).rlower })
            && cy == thisy.wrapping_add(1 as i32 as u32)
            && (unsafe { tty_term_has(term, TTYC_CUD1) }) != 0
        {
            tty_putcode(tty, TTYC_CUD1);
            current_block = 17422582042514589789;
        } else {
            change = thisy.wrapping_sub(cy) as i32;
            if (unsafe { abs(change) }) as u_int > cy
                || change < 0 && cy.wrapping_sub(change as u32) > (unsafe { (*tty).rlower })
                || change > 0 && cy.wrapping_sub(change as u32) < (unsafe { (*tty).rupper })
            {
                if (unsafe { tty_term_has(term, TTYC_VPA) }) != 0 {
                    tty_putcode_i(tty, TTYC_VPA, cy as i32);
                    current_block = 17422582042514589789;
                } else {
                    current_block = 16274275253645873319;
                }
            } else if change > 0 && (unsafe { tty_term_has(term, TTYC_CUU) }) != 0 {
                tty_putcode_i(tty, TTYC_CUU, change);
                current_block = 17422582042514589789;
            } else if change < 0 && (unsafe { tty_term_has(term, TTYC_CUD) }) != 0 {
                tty_putcode_i(tty, TTYC_CUD, -change);
                current_block = 17422582042514589789;
            } else {
                current_block = 16274275253645873319;
            }
        }
    } else {
        current_block = 16274275253645873319;
    }
    match current_block {
        16274275253645873319 => {
            tty_putcode_ii(tty, TTYC_CUP, cy as i32, cx as i32);
        }
        _ => {}
    }
    (unsafe { (*tty).cx = cx });
    (unsafe { (*tty).cy = cy });
}
extern "C" fn tty_hyperlink(mut tty: *mut tty, mut gc: *const grid_cell, mut hl: *mut hyperlinks) {
    let mut uri: *const i8 = std::ptr::null::<i8>();
    let mut id: *const i8 = std::ptr::null::<i8>();
    if (unsafe { (*gc).link }) == (unsafe { (*tty).cell.link }) {
        return;
    }
    (unsafe { (*tty).cell.link = (*gc).link });
    if hl.is_null() {
        return;
    }
    if (unsafe { (*gc).link }) == 0 as u32
        || (unsafe {
            hyperlinks_get(
                hl,
                (*gc).link,
                &mut uri,
                std::ptr::null_mut::<*const i8>(),
                &mut id,
            )
        }) == 0
    {
        tty_putcode_ss(
            tty,
            TTYC_HLS,
            b"\0" as *const u8 as *const i8,
            b"\0" as *const u8 as *const i8,
        );
    } else {
        tty_putcode_ss(tty, TTYC_HLS, id, uri);
    };
}
#[no_mangle]
pub extern "C" fn tty_attributes(
    mut tty: *mut tty,
    mut gc: *const grid_cell,
    mut defaults: *const grid_cell,
    mut palette: *mut colour_palette,
    mut hl: *mut hyperlinks,
) {
    let mut tc: *mut grid_cell = unsafe { &mut (*tty).cell };
    let mut gc2: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut changed: i32 = 0;
    (unsafe {
        memcpy(
            &mut gc2 as *mut grid_cell as *mut (),
            gc as *const (),
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    if !((unsafe { (*gc).flags }) as i32) & 0x20 as i32 != 0 {
        if gc2.fg == 8 {
            gc2.fg = unsafe { (*defaults).fg };
        }
        if gc2.bg == 8 {
            gc2.bg = unsafe { (*defaults).bg };
        }
    }
    if gc2.attr as i32 == (unsafe { (*tty).last_cell.attr }) as i32
        && gc2.fg == (unsafe { (*tty).last_cell.fg })
        && gc2.bg == (unsafe { (*tty).last_cell.bg })
        && gc2.us == (unsafe { (*tty).last_cell.us })
        && gc2.link == (unsafe { (*tty).last_cell.link })
    {
        return;
    }
    if (unsafe { tty_term_has((*tty).term, TTYC_SETAB) }) == 0 {
        if gc2.attr as i32 & 0x10 as i32 != 0 {
            if gc2.fg != 7 && !(gc2.fg == 8 || gc2.fg == 9) {
                gc2.attr = (gc2.attr as i32 & !(0x10 as i32)) as u_short;
            }
        } else if gc2.bg != 0 && !(gc2.bg == 8 || gc2.bg == 9) {
            gc2.attr = (gc2.attr as i32 | 0x10 as i32) as u_short;
        }
    }
    tty_check_fg(tty, palette, &mut gc2);
    tty_check_bg(tty, palette, &mut gc2);
    tty_check_us(tty, palette, &mut gc2);
    if (unsafe { (*tc).attr }) as i32 & !(gc2.attr as i32) != 0
        || (unsafe { (*tc).us }) != gc2.us && gc2.us == 0
    {
        tty_reset(tty);
    }
    tty_colours(tty, &mut gc2);
    changed = gc2.attr as i32 & !((unsafe { (*tc).attr }) as i32);
    (unsafe { (*tc).attr = gc2.attr });
    if changed & 0x1 as i32 != 0 {
        tty_putcode(tty, TTYC_BOLD);
    }
    if changed & 0x2 as i32 != 0 {
        tty_putcode(tty, TTYC_DIM);
    }
    if changed & 0x40 as i32 != 0 {
        tty_set_italics(tty);
    }
    if changed & (0x4 as i32 | 0x200 as i32 | 0x400 as i32 | 0x800 as i32 | 0x1000 as i32) != 0 {
        if changed & 0x4 as i32 != 0 || (unsafe { tty_term_has((*tty).term, TTYC_SMULX) }) == 0 {
            tty_putcode(tty, TTYC_SMUL);
        } else if changed & 0x200 as i32 != 0 {
            tty_putcode_i(tty, TTYC_SMULX, 2);
        } else if changed & 0x400 as i32 != 0 {
            tty_putcode_i(tty, TTYC_SMULX, 3);
        } else if changed & 0x800 as i32 != 0 {
            tty_putcode_i(tty, TTYC_SMULX, 4);
        } else if changed & 0x1000 as i32 != 0 {
            tty_putcode_i(tty, TTYC_SMULX, 5);
        }
    }
    if changed & 0x8 as i32 != 0 {
        tty_putcode(tty, TTYC_BLINK);
    }
    if changed & 0x10 as i32 != 0 {
        if (unsafe { tty_term_has((*tty).term, TTYC_REV) }) != 0 {
            tty_putcode(tty, TTYC_REV);
        } else if (unsafe { tty_term_has((*tty).term, TTYC_SMSO) }) != 0 {
            tty_putcode(tty, TTYC_SMSO);
        }
    }
    if changed & 0x20 as i32 != 0 {
        tty_putcode(tty, TTYC_INVIS);
    }
    if changed & 0x100 as i32 != 0 {
        tty_putcode(tty, TTYC_SMXX);
    }
    if changed & 0x2000 as i32 != 0 {
        tty_putcode(tty, TTYC_SMOL);
    }
    if changed & 0x80 as i32 != 0 && (unsafe { tty_acs_needed(tty) }) != 0 {
        tty_putcode(tty, TTYC_SMACS);
    }
    tty_hyperlink(tty, gc, hl);
    (unsafe {
        memcpy(
            &mut (*tty).last_cell as *mut grid_cell as *mut (),
            &mut gc2 as *mut grid_cell as *const (),
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
}
extern "C" fn tty_colours(mut tty: *mut tty, mut gc: *const grid_cell) {
    let mut tc: *mut grid_cell = unsafe { &mut (*tty).cell };
    if (unsafe { (*gc).fg }) == (unsafe { (*tc).fg })
        && (unsafe { (*gc).bg }) == (unsafe { (*tc).bg })
        && (unsafe { (*gc).us }) == (unsafe { (*tc).us })
    {
        return;
    }
    if (unsafe { (*gc).fg }) == 8
        || (unsafe { (*gc).fg }) == 9
        || ((unsafe { (*gc).bg }) == 8 || (unsafe { (*gc).bg }) == 9)
    {
        if (unsafe { tty_term_flag((*tty).term, TTYC_AX) }) == 0 {
            tty_reset(tty);
        } else {
            if ((unsafe { (*gc).fg }) == 8 || (unsafe { (*gc).fg }) == 9)
                && !((unsafe { (*tc).fg }) == 8 || (unsafe { (*tc).fg }) == 9)
            {
                tty_puts(tty, b"\x1B[39m\0" as *const u8 as *const i8);
                (unsafe { (*tc).fg = (*gc).fg });
            }
            if ((unsafe { (*gc).bg }) == 8 || (unsafe { (*gc).bg }) == 9)
                && !((unsafe { (*tc).bg }) == 8 || (unsafe { (*tc).bg }) == 9)
            {
                tty_puts(tty, b"\x1B[49m\0" as *const u8 as *const i8);
                (unsafe { (*tc).bg = (*gc).bg });
            }
        }
    }
    if !((unsafe { (*gc).fg }) == 8 || (unsafe { (*gc).fg }) == 9)
        && (unsafe { (*gc).fg }) != (unsafe { (*tc).fg })
    {
        tty_colours_fg(tty, gc);
    }
    if !((unsafe { (*gc).bg }) == 8 || (unsafe { (*gc).bg }) == 9)
        && (unsafe { (*gc).bg }) != (unsafe { (*tc).bg })
    {
        tty_colours_bg(tty, gc);
    }
    if (unsafe { (*gc).us }) != (unsafe { (*tc).us }) {
        tty_colours_us(tty, gc);
    }
}
extern "C" fn tty_check_fg(
    mut tty: *mut tty,
    mut palette: *mut colour_palette,
    mut gc: *mut grid_cell,
) {
    let mut r: u_char = 0;
    let mut g: u_char = 0;
    let mut b: u_char = 0;
    let mut colours: u_int = 0;
    let mut c: i32 = 0;
    if !((unsafe { (*gc).flags }) as i32) & 0x20 as i32 != 0 {
        c = unsafe { (*gc).fg };
        if c < 8
            && (unsafe { (*gc).attr }) as i32 & 0x1 as i32 != 0
            && (unsafe { tty_term_has((*tty).term, TTYC_NOBR) }) == 0
        {
            c += 90;
        }
        c = unsafe { colour_palette_get(palette, c) };
        if c != -(1 as i32) {
            (unsafe { (*gc).fg = c });
        }
    }
    if (unsafe { (*gc).fg }) & 0x2000000 as i32 != 0 {
        if (unsafe { (*(*tty).term).flags }) & 0x10 as i32 != 0 {
            return;
        }
        (unsafe { colour_split_rgb((*gc).fg, &mut r, &mut g, &mut b) });
        (unsafe { (*gc).fg = colour_find_rgb(r, g, b) });
    }
    if (unsafe { (*(*tty).term).flags }) & 0x1 as i32 != 0 {
        colours = 256 as u_int;
    } else {
        colours = (unsafe { tty_term_number((*tty).term, TTYC_COLORS) }) as u_int;
    }
    if (unsafe { (*gc).fg }) & 0x1000000 as i32 != 0 {
        if colours < 256 as u32 {
            (unsafe { (*gc).fg = colour_256to16((*gc).fg) });
            if (unsafe { (*gc).fg }) & 8 != 0 {
                (unsafe { (*gc).fg &= 7 });
                if colours >= 16 as u32 {
                    (unsafe { (*gc).fg += 90 });
                }
            }
        }
        return;
    }
    if (unsafe { (*gc).fg }) >= 90
        && (unsafe { (*gc).fg }) <= 97
        && colours < 16 as u32
    {
        (unsafe { (*gc).fg -= 90 });
        (unsafe { (*gc).attr = ((*gc).attr as i32 | 0x1 as i32) as u_short });
    }
}
extern "C" fn tty_check_bg(
    mut tty: *mut tty,
    mut palette: *mut colour_palette,
    mut gc: *mut grid_cell,
) {
    let mut r: u_char = 0;
    let mut g: u_char = 0;
    let mut b: u_char = 0;
    let mut colours: u_int = 0;
    let mut c: i32 = 0;
    if !((unsafe { (*gc).flags }) as i32) & 0x20 as i32 != 0 {
        c = unsafe { colour_palette_get(palette, (*gc).bg) };
        if c != -(1 as i32) {
            (unsafe { (*gc).bg = c });
        }
    }
    if (unsafe { (*gc).bg }) & 0x2000000 as i32 != 0 {
        if (unsafe { (*(*tty).term).flags }) & 0x10 as i32 != 0 {
            return;
        }
        (unsafe { colour_split_rgb((*gc).bg, &mut r, &mut g, &mut b) });
        (unsafe { (*gc).bg = colour_find_rgb(r, g, b) });
    }
    if (unsafe { (*(*tty).term).flags }) & 0x1 as i32 != 0 {
        colours = 256 as u_int;
    } else {
        colours = (unsafe { tty_term_number((*tty).term, TTYC_COLORS) }) as u_int;
    }
    if (unsafe { (*gc).bg }) & 0x1000000 as i32 != 0 {
        if colours < 256 as u32 {
            (unsafe { (*gc).bg = colour_256to16((*gc).bg) });
            if (unsafe { (*gc).bg }) & 8 != 0 {
                (unsafe { (*gc).bg &= 7 });
                if colours >= 16 as u32 {
                    (unsafe { (*gc).bg += 90 });
                }
            }
        }
        return;
    }
    if (unsafe { (*gc).bg }) >= 90
        && (unsafe { (*gc).bg }) <= 97
        && colours < 16 as u32
    {
        (unsafe { (*gc).bg -= 90 });
    }
}
extern "C" fn tty_check_us(
    mut tty: *mut tty,
    mut palette: *mut colour_palette,
    mut gc: *mut grid_cell,
) {
    let mut c: i32 = 0;
    if !((unsafe { (*gc).flags }) as i32) & 0x20 as i32 != 0 {
        c = unsafe { colour_palette_get(palette, (*gc).us) };
        if c != -(1 as i32) {
            (unsafe { (*gc).us = c });
        }
    }
    if (unsafe { tty_term_has((*tty).term, TTYC_SETULC1) }) == 0 {
        c = unsafe { colour_force_rgb((*gc).us) };
        if c == -(1 as i32) {
            (unsafe { (*gc).us = 8 });
        } else {
            (unsafe { (*gc).us = c });
        }
    }
}
extern "C" fn tty_colours_fg(mut tty: *mut tty, mut gc: *const grid_cell) {
    let mut tc: *mut grid_cell = unsafe { &mut (*tty).cell };
    let mut s: [i8; 32] = [0; 32];
    if (unsafe { (*tty).cell.fg }) >= 90
        && (unsafe { (*tty).cell.bg }) <= 97
        && ((unsafe { (*gc).fg }) < 90 || (unsafe { (*gc).fg }) > 97)
    {
        tty_reset(tty);
    }
    if (unsafe { (*gc).fg }) & 0x2000000 as i32 != 0
        || (unsafe { (*gc).fg }) & 0x1000000 as i32 != 0
    {
        if tty_try_colour(tty, unsafe { (*gc).fg }, b"38\0" as *const u8 as *const i8) != 0 {
            return;
        }
    } else if (unsafe { (*gc).fg }) >= 90 && (unsafe { (*gc).fg }) <= 97 {
        if (unsafe { (*(*tty).term).flags }) & 0x1 as i32 != 0 {
            (unsafe {
                xsnprintf(
                    s.as_mut_ptr(),
                    ::core::mem::size_of::<[i8; 32]>() as u64,
                    b"\x1B[%dm\0" as *const u8 as *const i8,
                    (*gc).fg,
                )
            });
            tty_puts(tty, s.as_mut_ptr());
        } else {
            tty_putcode_i(
                tty,
                TTYC_SETAF,
                (unsafe { (*gc).fg }) - 90 + 8,
            );
        }
    } else {
        tty_putcode_i(tty, TTYC_SETAF, unsafe { (*gc).fg });
    }
    (unsafe { (*tc).fg = (*gc).fg });
}
extern "C" fn tty_colours_bg(mut tty: *mut tty, mut gc: *const grid_cell) {
    let mut tc: *mut grid_cell = unsafe { &mut (*tty).cell };
    let mut s: [i8; 32] = [0; 32];
    if (unsafe { (*gc).bg }) & 0x2000000 as i32 != 0
        || (unsafe { (*gc).bg }) & 0x1000000 as i32 != 0
    {
        if tty_try_colour(tty, unsafe { (*gc).bg }, b"48\0" as *const u8 as *const i8) != 0 {
            return;
        }
    } else if (unsafe { (*gc).bg }) >= 90 && (unsafe { (*gc).bg }) <= 97 {
        if (unsafe { (*(*tty).term).flags }) & 0x1 as i32 != 0 {
            (unsafe {
                xsnprintf(
                    s.as_mut_ptr(),
                    ::core::mem::size_of::<[i8; 32]>() as u64,
                    b"\x1B[%dm\0" as *const u8 as *const i8,
                    (*gc).bg + 10,
                )
            });
            tty_puts(tty, s.as_mut_ptr());
        } else {
            tty_putcode_i(
                tty,
                TTYC_SETAB,
                (unsafe { (*gc).bg }) - 90 + 8,
            );
        }
    } else {
        tty_putcode_i(tty, TTYC_SETAB, unsafe { (*gc).bg });
    }
    (unsafe { (*tc).bg = (*gc).bg });
}
extern "C" fn tty_colours_us(mut tty: *mut tty, mut gc: *const grid_cell) {
    let mut tc: *mut grid_cell = unsafe { &mut (*tty).cell };
    let mut c: u_int = 0;
    let mut r: u_char = 0;
    let mut g: u_char = 0;
    let mut b: u_char = 0;
    if (unsafe { (*gc).us }) == 8 || (unsafe { (*gc).us }) == 9 {
        tty_putcode(tty, TTYC_OL);
    } else {
        if !(unsafe { (*gc).us }) & 0x2000000 as i32 != 0 {
            c = (unsafe { (*gc).us }) as u_int;
            if !c & 0x1000000 as i32 as u32 != 0 && (c >= 90 as u32 && c <= 97 as u32)
            {
                c = (c as u32).wrapping_sub(82 as i32 as u32) as u_int as u_int;
            }
            tty_putcode_i(tty, TTYC_SETULC1, (c & !(0x1000000 as i32) as u32) as i32);
            return;
        }
        (unsafe { colour_split_rgb((*gc).us, &mut r, &mut g, &mut b) });
        c = (65536 as i32 * r as i32 + 256 * g as i32 + b as i32) as u_int;
        if (unsafe { tty_term_has((*tty).term, TTYC_SETULC) }) != 0 {
            tty_putcode_i(tty, TTYC_SETULC, c as i32);
        } else if (unsafe { tty_term_has((*tty).term, TTYC_SETAL) }) != 0
            && (unsafe { tty_term_has((*tty).term, TTYC_RGB) }) != 0
        {
            tty_putcode_i(tty, TTYC_SETAL, c as i32);
        }
    }
    (unsafe { (*tc).us = (*gc).us });
}
extern "C" fn tty_try_colour(mut tty: *mut tty, mut colour: i32, mut type_0: *const i8) -> i32 {
    let mut r: u_char = 0;
    let mut g: u_char = 0;
    let mut b: u_char = 0;
    if colour & 0x1000000 as i32 != 0 {
        if (unsafe { *type_0 }) as i32 == '3' as i32
            && (unsafe { tty_term_has((*tty).term, TTYC_SETAF) }) != 0
        {
            tty_putcode_i(tty, TTYC_SETAF, colour & 0xff as i32);
        } else if (unsafe { tty_term_has((*tty).term, TTYC_SETAB) }) != 0 {
            tty_putcode_i(tty, TTYC_SETAB, colour & 0xff as i32);
        }
        return 0;
    }
    if colour & 0x2000000 as i32 != 0 {
        (unsafe { colour_split_rgb(colour & 0xffffff as i32, &mut r, &mut g, &mut b) });
        if (unsafe { *type_0 }) as i32 == '3' as i32
            && (unsafe { tty_term_has((*tty).term, TTYC_SETRGBF) }) != 0
        {
            tty_putcode_iii(tty, TTYC_SETRGBF, r as i32, g as i32, b as i32);
        } else if (unsafe { tty_term_has((*tty).term, TTYC_SETRGBB) }) != 0 {
            tty_putcode_iii(tty, TTYC_SETRGBB, r as i32, g as i32, b as i32);
        }
        return 0;
    }
    return -(1 as i32);
}
extern "C" fn tty_window_default_style(mut gc: *mut grid_cell, mut wp: *mut window_pane) {
    (unsafe {
        memcpy(
            gc as *mut (),
            &grid_default_cell as *const grid_cell as *const (),
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    (unsafe { (*gc).fg = (*wp).palette.fg });
    (unsafe { (*gc).bg = (*wp).palette.bg });
}
#[no_mangle]
pub extern "C" fn tty_default_colours(mut gc: *mut grid_cell, mut wp: *mut window_pane) {
    let mut oo: *mut options = unsafe { (*wp).options };
    let mut ft: *mut format_tree = std::ptr::null_mut::<format_tree>();
    (unsafe {
        memcpy(
            gc as *mut (),
            &grid_default_cell as *const grid_cell as *const (),
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    if (unsafe { (*wp).flags }) & 0x1000 as i32 != 0 {
        (unsafe { log_debug(b"%%%u: style changed\0" as *const u8 as *const i8, (*wp).id) });
        (unsafe { (*wp).flags &= !(0x1000 as i32) });
        ft = unsafe {
            format_create(
                std::ptr::null_mut::<client>(),
                std::ptr::null_mut::<cmdq_item>(),
                (CLIENT_ACTIVEPANE as u32 | (*wp).id) as i32,
                0x4 as i32,
            )
        };
        (unsafe {
            format_defaults(
                ft,
                std::ptr::null_mut::<client>(),
                std::ptr::null_mut::<session>(),
                std::ptr::null_mut::<winlink>(),
                wp,
            )
        });
        tty_window_default_style(unsafe { &mut (*wp).cached_active_gc }, wp);
        (unsafe {
            style_add(
                &mut (*wp).cached_active_gc,
                oo,
                b"window-active-style\0" as *const u8 as *const i8,
                ft,
            )
        });
        tty_window_default_style(unsafe { &mut (*wp).cached_gc }, wp);
        (unsafe {
            style_add(
                &mut (*wp).cached_gc,
                oo,
                b"window-style\0" as *const u8 as *const i8,
                ft,
            )
        });
        (unsafe { format_free(ft) });
    }
    if (unsafe { (*gc).fg }) == 8 {
        if wp == (unsafe { (*(*wp).window).active })
            && (unsafe { (*wp).cached_active_gc.fg }) != 8
        {
            (unsafe { (*gc).fg = (*wp).cached_active_gc.fg });
        } else {
            (unsafe { (*gc).fg = (*wp).cached_gc.fg });
        }
    }
    if (unsafe { (*gc).bg }) == 8 {
        if wp == (unsafe { (*(*wp).window).active })
            && (unsafe { (*wp).cached_active_gc.bg }) != 8
        {
            (unsafe { (*gc).bg = (*wp).cached_active_gc.bg });
        } else {
            (unsafe { (*gc).bg = (*wp).cached_gc.bg });
        }
    }
}
extern "C" fn tty_default_attributes(
    mut tty: *mut tty,
    mut defaults: *const grid_cell,
    mut palette: *mut colour_palette,
    mut bg: u_int,
    mut hl: *mut hyperlinks,
) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    (unsafe {
        memcpy(
            &mut gc as *mut grid_cell as *mut (),
            &grid_default_cell as *const grid_cell as *const (),
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    gc.bg = bg as i32;
    tty_attributes(tty, &mut gc, defaults, palette, hl);
}
extern "C" fn tty_clipboard_query_callback(
    mut _fd: i32,
    mut _events: i16,
    mut data: *mut (),
) {
    let mut tty: *mut tty = data as *mut tty;
    let mut c: *mut client = unsafe { (*tty).client };
    (unsafe { (*c).flags = ((*c).flags as u64 & !(CLIENT_ACTIVEPANE0 as u64)) as uint64_t });
    (unsafe { free((*c).clipboard_panes as *mut ()) });
    (unsafe { (*c).clipboard_panes = std::ptr::null_mut::<u_int>() });
    (unsafe { (*c).clipboard_npanes = 0 as u_int });
    (unsafe { (*tty).flags &= !(0x40 as i32) });
}
#[no_mangle]
pub extern "C" fn tty_clipboard_query(mut tty: *mut tty) {
    let mut tv: timeval = {
        let mut init = timeval {
            tv_sec: 5 as __time_t,
            tv_usec: 0,
        };
        init
    };
    if !(unsafe { (*tty).flags }) & 0x10 as i32 != 0 || (unsafe { (*tty).flags }) & 0x40 as i32 != 0
    {
        return;
    }
    tty_putcode_ss(
        tty,
        TTYC_MS,
        b"\0" as *const u8 as *const i8,
        b"?\0" as *const u8 as *const i8,
    );
    (unsafe { (*tty).flags |= 0x40 as i32 });
    (unsafe {
        event_set(
            &mut (*tty).clipboard_timer,
            -(1 as i32), 0 as i16,
            Some(
                tty_clipboard_query_callback
                    as unsafe extern "C" fn(i32, i16, *mut ()) -> (),
            ),
            tty as *mut (),
        )
    });
    (unsafe { event_add(&mut (*tty).clipboard_timer, &mut tv) });
}
